android 注册、登录实现程序
注册页面:
user_register.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_01">" <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="22dip"
android:textColor="#FFFFFF"
android:paddingLeft="140dip"
android:paddingRight="50dip"
android:paddingTop="10dip"
android:background="@drawable/topbg"
/>
"
<EditText
android:id="@+id/register_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@drawable/search"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:height="40dip"
android:hint="用户名"
/> <EditText
android:id="@+id/register_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@drawable/search"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:height="40dip"
android:hint="密码"
/> <EditText
android:id="@+id/reregister_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@drawable/search"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:height="40dip"
android:hint="确认密码"
/>
<Button
android:id="@+id/register_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/topbg"
android:height="40dip"
android:width="70dip"
android:layout_marginTop="60dip"
android:text="确定"
android:textColor="#FFFFFF"
android:textSize="22dip" /> </LinearLayout>
处理注册页面的Activity:
package com.example.foreveross.office; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import com.example.wenandroid.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class UserRegister extends Activity { private EditText register_username;
private EditText register_passwd;
private EditText reregister_passwd;
private Button register_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.user_register);
register_username=(EditText)findViewById(R.id.register_username);
register_passwd=(EditText)findViewById(R.id.register_passwd);
reregister_passwd=(EditText)findViewById(R.id.reregister_passwd);
register_submit=(Button)findViewById(R.id.register_submit);
register_username.setOnFocusChangeListener(new OnFocusChangeListener()
{ @Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
if(register_username.getText().toString().trim().length()<4){
Toast.makeText(UserRegister.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT).show();
}
}
} });
register_passwd.setOnFocusChangeListener(new OnFocusChangeListener()
{ @Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
if(register_passwd.getText().toString().trim().length()<6){
Toast.makeText(UserRegister.this, "密码不能小于8个字符", Toast.LENGTH_SHORT).show();
}
}
} });
reregister_passwd.setOnFocusChangeListener(new OnFocusChangeListener()
{ @Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
if(!reregister_passwd.getText().toString().trim().equals(register_passwd.getText().toString().trim())){
Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
}
}
} });
register_submit.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) { if(!checkEdit()){
return;
}
// TODO Auto-generated method stub
String httpUrl="http://192.168.1.100:8080/web-test/register.jsp";
HttpPost httpRequest=new HttpPost(httpUrl);
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",register_username.getText().toString().trim()));
params.add(new BasicNameValuePair("password",register_passwd.getText().toString().trim()));
HttpEntity httpentity = null;
try {
httpentity = new UrlEncodedFormEntity(params,"utf8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpRequest.setEntity(httpentity);
HttpClient httpclient=new DefaultHttpClient();
HttpResponse httpResponse = null;
try {
httpResponse = httpclient.execute(httpRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(httpResponse.getStatusLine().getStatusCode()==200)
{
String strResult = null;
try {
strResult = EntityUtils.toString(httpResponse.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UserRegister.this, strResult, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(UserRegister.this, "请求错误", Toast.LENGTH_SHORT).show();
} } });
} private boolean checkEdit(){
if(register_username.getText().toString().trim().equals("")){
Toast.makeText(UserRegister.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
}else if(register_passwd.getText().toString().trim().equals("")){
Toast.makeText(UserRegister.this, "密码不能为空", Toast.LENGTH_SHORT).show();
}else if(!register_passwd.getText().toString().trim().equals(reregister_passwd.getText().toString().trim())){
Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
}else{
return true;
}
return false;
} }
登录页面xml:
user_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_01"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="22dip"
android:textColor="#FFFFFF"
android:paddingLeft="140dip"
android:paddingRight="50dip"
android:paddingTop="10dip"
android:background="@drawable/topbg"
/> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <EditText
android:id="@+id/login_username"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:hint="用户名"
android:paddingTop="10dip"
android:textSize="18dip"
android:background="@drawable/search"> </EditText> <EditText
android:id="@+id/login_password"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip"
android:password="true"
android:paddingTop="10dip"
android:textSize="18dip"
android:hint="密码"
android:background="@drawable/search"> </EditText>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dip"> <CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dip"
android:layout_marginRight="30dip"
android:text="记住密码"
android:button="@drawable/checkbox_icon_no" />"
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:paddingRight="50dip"
android:button="@drawable/checkbox_icon_no"/>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip">
<Button
android:id="@+id/user_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginLeft="50dip"
android:textColor="#F7FBFD"
android:background="#FF0000"
android:width="70dip"
android:height="40dip"
android:textSize="18dip"
/> <Button
android:id="@+id/user_register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginLeft="50dip"
android:textColor="#F7FBFD"
android:width="70dip"
android:height="40dip"
android:background="#0F9000"
android:textSize="18dip"
/> </LinearLayout> </LinearLayout>
登录页面Activity:
package com.example.foreveross.office; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import com.example.wenandroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class UserLogin extends Activity implements OnClickListener {
private EditText login_username;
private EditText login_password;
private Button user_login_button;
private Button user_register_button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.user_login);
initWidget(); }
private void initWidget()
{
login_username=(EditText)findViewById(R.id.login_username);
login_password=(EditText)findViewById(R.id.login_password);
user_login_button=(Button)findViewById(R.id.user_login_button);
user_register_button=(Button)findViewById(R.id.user_register_button);
user_login_button.setOnClickListener(this);
user_register_button.setOnClickListener(this);
login_username.setOnFocusChangeListener(new OnFocusChangeListener()
{ @Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
String username=login_username.getText().toString().trim();
if(username.length()<4){
Toast.makeText(UserLogin.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT);
}
}
} });
login_password.setOnFocusChangeListener(new OnFocusChangeListener()
{ @Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
String password=login_password.getText().toString().trim();
if(password.length()<4){
Toast.makeText(UserLogin.this, "密码不能小于4个字符", Toast.LENGTH_SHORT);
}
}
} });
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.user_login_button:
if(checkEdit())
{
login();
} break;
case R.id.user_register_button:
Intent intent2=new Intent(UserLogin.this,UserRegister.class);
startActivity(intent2);
break;
}
} private boolean checkEdit(){
if(login_username.getText().toString().trim().equals("")){
Toast.makeText(UserLogin.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
}else if(login_password.getText().toString().trim().equals("")){
Toast.makeText(UserLogin.this, "密码不能为空", Toast.LENGTH_SHORT).show();
}else{
return true;
}
return false;
} private void login(){
String httpUrl="http://192.168.1.102:8080/web-test/login.jsp";
HttpPost httpRequest=new HttpPost(httpUrl);
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",login_username.getText().toString().trim()));
params.add(new BasicNameValuePair("password",login_password.getText().toString().trim()));
HttpEntity httpentity = null;
try {
httpentity = new UrlEncodedFormEntity(params,"utf8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpRequest.setEntity(httpentity);
HttpClient httpclient=new DefaultHttpClient();
HttpResponse httpResponse = null;
try {
httpResponse = httpclient.execute(httpRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(httpResponse.getStatusLine().getStatusCode()==200)
{
String strResult = null;
try {
strResult = EntityUtils.toString(httpResponse.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UserLogin.this, strResult, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(UserLogin.this,IndexActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(UserLogin.this, "登录失败!", Toast.LENGTH_SHORT).show();
} }
}
登录成功则跳转到IndexActivity.java
android 注册、登录实现程序的更多相关文章
- 基于后端云的Android注册登录开发
APP开发离不开注册登录功能,但是注册登录功能开发需要后台数据库的支持,对于一些初学者或者对后台数据 不熟悉的同学来说可能会有些困难.本文介绍一下后端云: 1. Bmob是国内起步较早的云后端服务平台 ...
- python实现注册登录小程序
用python 实现模拟注册和登录的程序:用户信息最终以字典的格式储存在一个txt文件里,具体实现如下: users.txt里用户字典格式如下: { '}, '}, '} } # 注册 f = ope ...
- Android开发案例 - 注册登录
本文只涉及UI方面的内容, 如果您是希望了解非UI方面的访客, 请跳过此文. 在微博, 微信等App的注册登录过程中有这样的交互场景(如下图): 打开登录界面 在登录界面中, 点击注册, 跳转到注册界 ...
- Android:注册登录
注册登录的实现 先在layout里新建一个xml文件: //login.xml <?xml version="1.0" encoding="utf-8"? ...
- 微信小程序-----校园头条详细开发之注册登录
1.注册登录功能的实现 1.1结构 1.2 代码实现 1.2.1 为了通信的安全着想,在此我是通过小程序端获得code,然后传递给后端,在后端向微信后台发送api请求,解密,从而得到用户的唯一标示o ...
- 小程序&app 注册登录、绑定
前段时间开发中的一款产品,有小程序和app:小程序直接微信登录,app使用手机号+验证码注册,手机号+验证码/密码登录. 用户使用其中一套账号密码即可正常使用,不强制要求完善另一套账号.为避免同一用户 ...
- Android 比较好看的注册登录界面
各位看官姥爷: 对于一款android手机app而言,美观的界面使得用户有好的使用体验,而一款好看的注册登录界面也会给用户好的用户体验,那么话不多说,直接上代码 首先是一款简单的界面展示 1.登陆界面 ...
- android 第三方登录---新浪微博
1.AndroidManiFest.xml设置,这里我只是简单的用授权,获取基本信息,所以只用了这一个 <!--微博--> <!-- 必须注册在微博授权,分享微博时候用到 --> ...
- Android SharedPreferences登录记住密码
SharedPreferences是Android中存储简单数据的一个工具类.可以想象它是一个小小的Cookie,它通过用键值对的方式把简单 数据类型(boolean.int.float.long和S ...
随机推荐
- Ubuntu14.04下Python3.4启动IDLE
1.在Ubuntu14.04 LTS版本中,已经自行安装了python,可以在Terminal(CTRL+ALT+T)中输入:ls /usr/bin | grep python 进行查看. 如果想运行 ...
- CMDB反思2
当云灭掉CMDB http://blog.vsharing.com/xqscool/A1193910.html 虽然之前也思考过当运维底层都被替换为云时,现有的传统运维可能就消失了,其所依赖的ITIL ...
- 根据给定的日期给 dateEdit 控件增加颜色
private void dateEdit1_DrawItem(object sender, DevExpress.XtraEditors.Calendar.CustomDrawDayNumberCe ...
- 在xcode上搭建OpenGL3.x运行环境
最近开始学习OpenGL,网上的教程太散乱,于是打算照着红宝书<OpenGL编程指南(第七版)>来学习. 于是在Mac上搭建一下Demo环境.比较方便的是,OS X上已经装了OpenGL ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...
- Python 学习笔记(四)正则、闭合、生成器
(一)正则表达式 基本规则: ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b 匹配一个单词边界. \d 匹配一个数字. \D 匹配一个任意的非数字字符. x? 匹配可选的x字符.换句话说,就 ...
- 通用表表达式(Common Table Expression)
问题:编写由基本的 SELECT/FROM/WHERE 类型的语句派生而来的复杂 SQL 语句. 方案1:编写在From子句内使用派生表(内联视图)的T-SQL查询语句. 方案2:使用视图 方案3:使 ...
- 【hbase】使用thrift with python 访问HBase
HBase 版本: 0.98.6 thrift 版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: Traceback (most recent ...
- android sdk manager 闪退 打不开问题
android sdk manager 闪退 打不开问题 环境 win8系统 如果访问不了 dl-ssl.google.com 网址,在C:\Windows\System32\Drivers\etc ...
- TCL/Expect交互式自动化测试概要 - - ITeye技术网站
TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行"可程序化"会话的脚本语言,是一种可以提供"分支和嵌 ...