利用Android常用UI控件完成简单地注册功能
来源:互联网 发布:client是什么软件 编辑:程序博客网 时间:2024/05/17 22:56
最近在学习Android,刚刚看到UI控件这里,自己写了个小例子和初学者分享一下。
实现的是一个注册页面,需要填写用户名(EditText),密码(EditText),年龄(EditText),性别(RadioButton),爱好(CheckBox),城市(Spinner)。其中用户名,密码,年龄不能为空,需要有验证;点击注册按钮后弹出一个确认窗口(AlertDialog),窗口中显示确认的信息(也就是刚才填写的信息),并且有确认和取消键;点击确认信息4秒后可以完成注册,过程和结果都要有提示。
具体效果如下所示:
具体代码实现如下:
regist.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/name" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/name"/> <EditText android:id="@+id/nameValue" android:inputType="text" android:layout_toRightOf="@id/name" android:layout_toEndOf="@id/name" android:layout_width="@dimen/editText" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/password" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/password"/> <EditText android:id="@+id/passwordValue" android:inputType="textPassword" android:layout_toRightOf="@id/password" android:layout_toEndOf="@id/password" android:layout_width="@dimen/editText" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/age" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/age"/> <EditText android:id="@+id/ageValue" android:inputType="number" android:layout_toRightOf="@id/age" android:layout_toEndOf="@id/age" android:layout_width="@dimen/editText" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/sex" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/sex"/> <RadioGroup android:id="@+id/sexMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/sex" android:layout_toEndOf="@id/sex" android:checkedButton="@+id/man" android:orientation="horizontal"> <RadioButton android:id="@id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/man"/> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/woman"/> </RadioGroup></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/favorite" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/favorite"/> <CheckBox android:id="@+id/pingpang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/favorite" android:layout_toEndOf="@id/favorite" android:text="@string/pingpang"/> <CheckBox android:id="@+id/football" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/pingpang" android:layout_toEndOf="@id/pingpang" android:text="@string/football"/> <CheckBox android:id="@+id/badminton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/favorite" android:layout_toEndOf="@id/favorite" android:layout_below="@id/football" android:text="@string/badminton"/> <CheckBox android:id="@+id/basketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/badminton" android:layout_toEndOf="@id/badminton" android:layout_below="@id/football" android:text="@string/basketball"/></RelativeLayout><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/city" android:layout_width="@dimen/textView" android:layout_height="wrap_content" android:text="@string/city"/> <Spinner android:id="@+id/cityValue" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/city" android:layout_toEndOf="@id/city"/></RelativeLayout><Button android:id="@+id/regist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/regist"/></LinearLayout>strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">猴赛累</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="name">用户名:</string> <string name="password">密码:</string> <string name="age">年龄:</string> <string name="sex">性别:</string> <string name="favorite">爱好:</string> <string name="city">城市:</string> <string name="regist">注册</string> <string name="man">男</string> <string name="woman">女</string> <string name="pingpang">乒乓球</string> <string name="football">足球</string> <string name="badminton">羽毛球</string> <string name="basketball">篮球</string></resources>
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="textView">70dp</dimen> <dimen name="editText">200dp</dimen></resources>
MainActivity.java
package com.example.housailei;import java.util.ArrayList;import java.util.List;import android.annotation.SuppressLint;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends Activity {private boolean flag=true;private static final String[] citis={"济南","北京","上海","成都","广州","天津","杭州","深圳"};private EditText name,password,age;private RadioGroup sex;private List<CheckBox> favorites;private CheckBox pingpang,football,badminton,basketball;private Spinner city;private Button regist;private ProgressDialog progressDialog; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.regist);favorites=new ArrayList<CheckBox>();name=(EditText) findViewById(R.id.nameValue);password=(EditText) findViewById(R.id.passwordValue);age=(EditText) findViewById(R.id.ageValue);sex=(RadioGroup) findViewById(R.id.sexMenu);pingpang=(CheckBox) findViewById(R.id.pingpang);football=(CheckBox) findViewById(R.id.football);badminton=(CheckBox) findViewById(R.id.badminton);basketball=(CheckBox) findViewById(R.id.basketball);city=(Spinner) findViewById(R.id.cityValue);regist=(Button) findViewById(R.id.regist);favorites.add(pingpang);favorites.add(football);favorites.add(badminton);favorites.add(basketball);//创建数组型适配器ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,citis);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);city.setAdapter(adapter);//为注册按钮设置点击监听regist.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){flag=checkUser();if(flag){//创建AlertDialog对话框显示要确认的注册信息new AlertDialog.Builder(MainActivity.this).setTitle("请确认注册信息:").setMessage("用户名:"+name.getText().toString()+"\n"+"密码:"+password.getText().toString()+"\n"+"年龄:"+age.getText().toString()+"\n"+"性别:"+getSex()+"\n"+"爱好:"+getFavorite()+"\n"+"城市:"+getCity()+"\n").setCancelable(false).setPositiveButton("确定",new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int id){progressDialog=ProgressDialog.show(MainActivity.this, "用户信息注册中", "请等待...", true, false);//创建一个新线程new Thread(){ @Override public void run() { //需要花时间计算的方法 Calculation.calculate(4); //向handler发消息 handler.sendEmptyMessage(0); }}.start();}}).setNegativeButton("取消",new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int id){dialog.cancel();}}).show();}}});}private Handler handler = new Handler(){ @SuppressLint("HandlerLeak")@Override public void handleMessage(Message msg) { //关闭ProgressDialog progressDialog.dismiss(); //利用Toast提示注册成功 Toast.makeText(MainActivity.this, "注册成功!", Toast.LENGTH_SHORT).show(); }}; //获取城市信息protected String getCity() {return citis[city.getSelectedItemPosition()];}//获取爱好信息protected String getFavorite() {String favorite="";for(CheckBox cb:favorites){if(cb.isChecked()){favorite+=cb.getText().toString();favorite+=",";}}if(favorite!=""){favorite.subSequence(0, favorite.length()-1);}else{//如果没有勾选爱好,则返回该提示。favorite="您未选择爱好!";}return favorite;}//获取性别信息protected String getSex() {RadioButton sexChecked=(RadioButton) findViewById(sex.getCheckedRadioButtonId());return sexChecked.getText().toString();}//检验用户名,密码,年龄是否为空,如果有空的则提示不能为空,并返回falseprotected boolean checkUser() {if(name.getText().toString().length()==0){name.setError("用户名不能为空!");return false;}if(password.getText().toString().length()==0){password.setError("密码不能为空!");return false;}if(age.getText().toString().length()==0){age.setError("年龄不能为空!");return false;}return true;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
Calculation.java
package com.example.housailei;public class Calculation {public static void calculate(int sleepSeconds){ try { Thread.sleep(sleepSeconds * 1000); } catch (Exception e) { // TODO: handle exception } } }
0 0
- 利用Android常用UI控件完成简单地注册功能
- Android常用UI控件 ViewGroup
- android常用UI控件总结
- Android UI开发常用控件
- Android 常用UI控件收藏
- Android开发-UI控件:实现登陆页面并跳转和简单的注册页面
- Android开发(14)-- 多线程中实现利用自定义控件绘制小球并完成小球自动下落的功能
- Android开发(14)-- 多线程中实现利用自定义控件绘制小球并完成小球自动下落的功能
- Android中常用的UI控件
- Android中常用的UI控件
- 常用的UI控件(android)
- Android 常用布局及基本UI控件
- Android-常用UI控件(Spinner/AutoCompleteTextView)
- Android UI概述和常用控件
- Android 常用布局及基本UI控件
- android UI开发及常用控件
- Android UI开发中的常用控件TextView
- Android UI开发中的常用控件Button
- Android Fragment 中自定义键盘如何调整布局,不挡住界面控件
- spine3.0导出文件转换成cocos3.8可使用文件工具
- Mybatis-spring整合对session的理解
- 途牛原创|Android 中有限状态机的设计和应用
- 1004 Counting Leaves
- 利用Android常用UI控件完成简单地注册功能
- Android逆向之旅---解析编译之后的classes.dex文件格式
- 史上最全Windows版本搭建安装React Native环境配置
- CentOS 6.5 搭建 Zabbix
- andbug出permission denied
- python mysql
- 详解 CALayer 和 UIView 的区别和联系
- 面试后心得
- hdu 3706