您的位置:首页 > 移动开发 > Android开发

Android简单的用户信息注册界面

2014-06-11 22:51 1101 查看
一、运行测试效果



二、基本组件和原理 

1、基本组件:AndroidUI、View、TextView、EditText、Button、RadioGroup、ViewSwitcher、动画AnimationDrawable、Animation、事件监听器OnClickListener、线程Thread及Handler刷新界面等。

2、原理:输入用户名、密码、邮箱、昵称可以点击旁边的button来判断可用性(程序中假设均可用,显示正确符号),在用户输入的所有信息都不为空时,点击button执行注册验证,启动动画,在新线程中模拟从远程验证用户信息的方法(程序中sleep(2000)假设验证失败),刷新界面组件。



三、具体实现

package com.ui;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.example.appmainui.R;
import com.widget.BaseActivity;

/**
* @name 用户注册页面
* @descripation 输入用户信息,验证信息正确性,信息入库
* @author 樊俊彬
* @date 2014-6-9
* @version 1.0
*/
public class RegistryUserInfoActivity extends BaseActivity {

private EditText registInputUserName;
private EditText registInputPassword;
private EditText registInputEmail;
private EditText registInputNickName;
private RadioGroup registInputSex;
private EditText registInputAge;
private EditText registInputPhone;

private Button registButtonCheckUserName;
private Button registButtonCheckPassword;
private Button registButtonCheckEmail;
private Button registButtonCheckNickName;
private Button registButtonRegister;

private ViewSwitcher registViewSwitcher;
private View registView;
private AnimationDrawable registAnimation;

private boolean isRegistSuccess = false;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regist_user_info);
initUI();
}

/**
* 初始化界面组件
*/
private void initUI() {
// 用户输入的信息
registInputUserName = (EditText) findViewById(R.id.registInputUserName);
registInputPassword = (EditText) findViewById(R.id.registInputPassword);
registInputEmail = (EditText) findViewById(R.id.registInputEmail);
registInputNickName = (EditText) findViewById(R.id.registInputNickName);
registInputSex = (RadioGroup) findViewById(R.id.registInputSex);
registInputAge = (EditText) findViewById(R.id.registInputAge);
registInputPhone = (EditText) findViewById(R.id.registInputPhone);
registViewSwitcher = (ViewSwitcher) findViewById(R.id.registViewSwitcher);
registView = (View) findViewById(R.id.registViewLoading);
// 检测用户名可用性按钮
registButtonCheckUserName = (Button) findViewById(R.id.registButtonCheckUserName);
registButtonCheckUserName.setOnClickListener(checkAvailabilityListener(
registButtonCheckUserName, registInputUserName));

// 检测密码可用性按钮
registButtonCheckPassword = (Button) findViewById(R.id.registButtonCheckPassword);
registButtonCheckPassword.setOnClickListener(checkAvailabilityListener(
registButtonCheckPassword, registInputPassword));

// 检测Email可用性
registButtonCheckEmail = (Button) findViewById(R.id.registButtonCheckEmail);
registButtonCheckEmail.setOnClickListener(checkAvailabilityListener(
registButtonCheckEmail, registInputEmail));

// 检测昵称可用性
registButtonCheckNickName = (Button) findViewById(R.id.registButtonCheckNickName);
registButtonCheckNickName.setOnClickListener(checkAvailabilityListener(
registButtonCheckNickName, registInputNickName));
// 注册用户信息
registButtonRegister = (Button) findViewById(R.id.registButtonRegister);
registButtonRegister.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

String inputUserName = registInputUserName.getText().toString();
String inputPassword = registInputPassword.getText().toString();
String inputEmail = registInputEmail.getText().toString();
String inputNickName = registInputNickName.getText().toString();
String inputSex = "";
String inputAge = registInputAge.getText().toString();
String inputPhone = registInputPhone.getText().toString();

// 开启动画,执行注册验证
registAnimation = (AnimationDrawable) registView
.getBackground();
registAnimation.start();
registViewSwitcher.showNext();
registUserInfo(inputUserName, inputPassword, inputEmail, inputNickName, inputSex, inputAge, inputPhone);
}
});
}

/**
* 检测可用性按钮事件监听器
*
* @param button
* @param editText
* @return
*/
public View.OnClickListener checkAvailabilityListener(final Button button,
final EditText editText) {
return new OnClickListener() {
@Override
public void onClick(View arg0) {

// 输入不能为空
if (editText.getText().toString().equals("")) {
button.setBackgroundResource(R.drawable.icon_cool_false);
} else {
button.setBackgroundResource(R.drawable.icon_cool_true);
}
}
};
}

/**
* 启动新线程,注册验证用户输入的信息
*
* @param inputUserName
* @param inputPassword
* @param Email
* @param NickName
* @param Sex
* @param Age
* @param Phone
*/
protected void registUserInfo(String inputUserName, String inputPassword,
String inputEmail, String inputNickName, String inputSex, String inputAge, String inputPhone) {

// 刷新界面
final Handler handler = new Handler() {
public void handleMessage(Message msg) {

// 注册成功
if (msg.what == 1) {
Toast.makeText(RegistryUserInfoActivity.this, "注册成功",
Toast.LENGTH_SHORT).show();
finish();
}
// 注册失败
if (msg.what == 0) {
Toast.makeText(RegistryUserInfoActivity.this, "注册失败",
Toast.LENGTH_SHORT).show();
registViewSwitcher.showPrevious();
}
}
};

// 登陆验证线程
new Thread() {
@Override
public void run() {
super.run();
Message msg = new Message();
try {
// 验证输入的信息
sleep(2000);
isRegistSuccess = false;

if (isRegistSuccess) {
// 验证成功
msg.what = 1;
} else {
// 验证失败
msg.what = 0;
}
} catch (InterruptedException e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
handler.sendMessage(msg);
}
}.start();

}
}


<?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" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@drawable/head_bg_registry"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="用户注册"
android:textColor="@color/white"
android:textSize="@dimen/text_size_23" />
</RelativeLayout>

<!-- 信息输入框 -->

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip"
android:layout_marginTop="20.0dip"
android:background="@drawable/shape_bg_regist_user_info"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="用户名:" />

<EditText
android:id="@+id/registInputUserName"
style="@style/regist_user_info_input"
android:hint="字母/数字/符号的任意组合" />

<Button
android:id="@+id/registButtonCheckUserName"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="10dip"
android:background="@drawable/icon_cool_check" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="密    码:" />

<EditText
android:id="@+id/registInputPassword"
style="@style/regist_user_info_input"
android:hint="密码长度不超过16位"
android:password="true" />

<Button
android:id="@+id/registButtonCheckPassword"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="10dip"
android:background="@drawable/icon_cool_check" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="邮箱号:" />

<EditText
android:id="@+id/registInputEmail"
style="@style/regist_user_info_input"
android:hint="也可作登陆用户名使用" />

<Button
android:id="@+id/registButtonCheckEmail"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="10dip"
android:background="@drawable/icon_cool_check" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="昵   称:" />

<EditText
android:id="@+id/registInputNickName"
style="@style/regist_user_info_input"
android:hint="个性点的好" />

<Button
android:id="@+id/registButtonCheckNickName"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="10dip"
android:background="@drawable/icon_cool_check" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="性    别:" />

<RadioGroup
android:id="@+id/registInputSex"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="15.0dip"
android:background="@android:color/white"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/reg_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="帅男"
android:textColor="@android:color/black"
android:textSize="18.0sp" />

<RadioButton
android:id="@+id/reg_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60.0dip"
android:checked="false"
android:text="美女"
android:textColor="@android:color/black"
android:textSize="18.0sp" />
</RadioGroup>
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="年    龄:" />

<EditText
android:id="@+id/registInputAge"
style="@style/regist_user_info_input"
android:hint="点击输入数字"
android:phoneNumber="true" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
style="@style/regist_user_info_text_view"
android:text="手机号:" />

<EditText
android:id="@+id/registInputPhone"
style="@style/regist_user_info_input"
android:hint="方便与您联系"
android:phoneNumber="true" />
</LinearLayout>

<View style="@style/regist_user_info_cutline_view" />
</LinearLayout>

<!-- 提示文本 -->

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20.0dip"
android:layout_marginRight="20.0dip"
android:layout_marginTop="10.0dip"
android:gravity="left"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register_tip"
android:textColor="@color/black"
android:textSize="@dimen/text_size_12" />
</LinearLayout>

<ViewSwitcher
android:id="@+id/registViewSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<!-- 注册按钮 -->

<Button
android:id="@+id/registButtonRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30.0dip"
android:layout_marginRight="30.0dip"
android:layout_marginTop="20.0dip"
android:background="@drawable/btn_bg_regist_user_info"
android:text="注      册"
android:textColor="#ff000000"
android:textSize="@dimen/text_size_21" />

<View
android:id="@+id/registViewLoading"
android:layout_width="120.0dip"
android:layout_height="120.0dip"
android:layout_gravity="center"
android:background="@anim/login_loading" />
</ViewSwitcher>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息