实现注册功能,首先输入用户帐号,然后连续输入密码两次,并匹配是否相同
(1)给出注册的XML文档
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar" >
<LinearLayout
android:id="@+id/login_ll_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="6dip"
android:paddingBottom="6dip"
android:paddingRight="15dip"
android:paddingLeft="15dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_find_password"
android:layout_width="35dip"
android:layout_height="35dip"
android:background="@drawable/test_return"/>
</LinearLayout>
<TextView
android:id="@+id/text_zhaohuimima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/change_password"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="21.0dip"
android:paddingRight="21.0dip"
android:paddingTop="18.0dip" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/login_edit_userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:hint="@string/original_password"
android:inputType="textPassword"
android:paddingRight="40.0dip"
android:singleLine="true" />
<ImageView
android:id="@+id/login_image_userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/note_delete_fork"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/login_text_change"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="12.0dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/login_edit_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="@string/new_password"
android:paddingRight="40.0dip"
android:inputType="textPassword"
android:singleLine="true" />
<ImageView
android:id="@+id/login_image_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/note_delete_fork"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/login_text_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="12.0dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/login_edit_passwordAgain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/again_new_password"
android:paddingRight="40.0dip"
android:inputType="textPassword"
android:singleLine="true" />
<ImageView
android:id="@+id/login_image_passwordAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/note_delete_fork"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/login_text_passwordAgain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="12.0dip" />
<Button
android:id="@+id/login_btn_register"
android:layout_width="fill_parent"
android:layout_height="42.0dip"
android:layout_marginTop="21.0dip"
android:background="@drawable/btn_style_green"
android:text="@string/change_password"
android:textColor="#ffffff"
android:textSize="18.0dip" />
<ProgressBar
android:id="@+id/change_pwd_progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</ScrollView>
(2)registerActivity.java,其中包括对用户帐号和密码输入内容的验证,以及两次密码是否相同的验证,并实时给出了相应的提示。
package com.dragonldg.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class registerActivity extends Activity implements
OnClickListener {
private String Tag = "registerActivity";// 日志显示信息
private String data = null;
LinearLayout login_ll_return;
EditText login_edit_userName;//用户帐号
EditText login_edit_password;//首次用户密码
EditText login_edit_passwordAgain;//第二次用户密码
Button login_btn_register;//注册按钮
TextView login_text_change;//输入用户帐号时的实时提示消息
TextView login_text_change_password;//输入用户密码时的实时提示消息
TextView login_text_passwordAgain;
ImageView login_image_userName;//当有内容时单击图片清空控件
ImageView login_image_password;
ImageView login_image_passwordAgain;
ProgressBar change_pwd_progressBar;//单击注册按钮显示注册进度条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏设置
setContentView(R.layout.activity_change_password);
Global.mContext = this;
initComponent();
registerListener();
init();
}
private void initComponent() {
login_btn_register = (Button) findViewById(R.id.login_btn_register);
login_edit_userName = (EditText) findViewById(R.id.login_edit_userName);
login_edit_password = (EditText) findViewById(R.id.login_edit_password);
login_edit_passwordAgain = (EditText) findViewById(R.id.login_edit_passwordAgain);
login_text_change = (TextView) findViewById(R.id.login_text_change);
login_text_password = (TextView) findViewById(R.id.login_text_password);
login_text_passwordAgain = (TextView) findViewById(R.id.login_text_passwordAgain);
login_image_userName = (ImageView) findViewById(R.id.login_image_userName);
login_image_password = (ImageView) findViewById(R.id.login_image_password);
login_image_passwordAgain = (ImageView) findViewById(R.id.login_image_passwordAgain);
change_pwd_progressBar = (ProgressBar) findViewById(R.id.change_pwd_progressBar);
}
private void registerListener() {
login_btn_register.setOnClickListener(this);
login_image_userName.setOnClickListener(this);
login_image_password.setOnClickListener(this);
login_image_passwordAgain.setOnClickListener(this);
}
private void init() {
login_btn_register.setEnabled(false);
//用户帐号
login_edit_userName.setOnFocusChangeListener(new OnFocusChangeListener() {//焦点变化监听事件
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
login_image_userName.setVisibility(View.VISIBLE);
} else {
login_image_userName.setVisibility(View.GONE);
}
}
});
login_edit_userName.addTextChangedListener(new TextWatcher() {//文本内容改变监听事件
private CharSequence temp;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
temp = s;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
login_text_change.setText("");
if (temp.length() > 0) {
if (temp.length() < 3) {
login_text_change.setText("用户帐号长度至少3个字符");
}
if (temp.length() > 20) {
login_text_change.setText("用户帐号长度最长不超过20个字符");
}
}
}
});
//用户密码,首次
login_edit_password
.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
login_image_password
.setVisibility(View.VISIBLE);
} else {
login_image_password
.setVisibility(View.GONE);
}
}
});
login_edit_password.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
temp = s;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
login_text_change_password.setText("");
if (temp.length() > 0) {
if (temp.length() < 3) {
login_text_change_password.setText("密码长度至少3个字符");
}
if (temp.length() > 20) {
login_text_change_password.setText("密码长度最长不超过20个字符");
}
}
}
});
//用户密码,第二次
login_edit_passwordAgain
.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
login_image_passwordAgain
.setVisibility(View.VISIBLE);
} else {
login_image_passwordAgain
.setVisibility(View.GONE);
}
}
});
login_edit_passwordAgain.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
temp = s;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
String str1;
String str2;
str1 = login_edit_password.getText().toString();
str2 = login_edit_passwordAgain.getText().toString();
if (str1.equals(str2)) {
login_text_passwordAgain.setText("");
if (temp.length() >= 3 && temp.length() <= 20) {
login_btn_register.setEnabled(true);
}
} else {
login_text_passwordAgain.setText("两次输入的密码不一致,请重新输入");
login_btn_register.setEnabled(false);// 密码不同时保证不能点击
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_image_userName:
login_edit_userName.setText("");
break;
case R.id.login_image_password:
login_edit_password.setText("");
login_edit_passwordAgain.setText("");
login_text_passwordAgain.setText("");
login_btn_register.setEnabled(false);
break;
case R.id.login_image_passwordAgain:
login_edit_passwordAgain.setText("");
login_text_passwordAgain.setText("");
login_btn_register.setEnabled(false);
break;
case R.id.login_btn_register:
change_pwd_progressBar.setVisibility(View.VISIBLE);
login_btn_register.setEnabled(false);
break;
default:
break;
}
}
}
(3)运行结果,主要是利用了几种监听事件,控件焦点变化监听事件、控件内容变化监听事件、控件单击响应事件。