Android实现简单用户注册案例
程序员文章站
2022-09-06 13:52:54
本文实例为大家分享了android实现简单用户注册的具体代码,供大家参考,具体内容如下目标: 设计一个用户注册案例。在主界面中对输入的手机号、密码、性别、爱好和城市后,可以在界面二中进行显示。提示:1...
本文实例为大家分享了android实现简单用户注册的具体代码,供大家参考,具体内容如下
目标: 设计一个用户注册案例。在主界面中对输入的手机号、密码、性别、爱好和城市后,可以在界面二中进行显示。
提示:
1、页面布局的元素用到textview、edittext、button、radiobutton、checkbox、spinner;
2、通过intent实现主界面跳转到界面二
3、涉及传递多个的数据时,使用bundle对象作为容器,通过调用bundle的putstring先将数据存储到bundle中,然后调用intent的putextras()方法将bundle存入intent中,然后获得intent后, 调用getextras()获得bundle容器,然后调用其getstring获取对应的数据!
bundle bundle=new bundle(); bundle.putstring("phone",phone_str); bundle.putstring("paswd",paswd_str); bundle.putstring("sex",sex_str); bundle.putstring("hobby",hobby_str); bundle.putstring("city",city_str); //为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换 intent.putextras(bundle);
//取得启动该activity的intent对象 intent intent=this.getintent(); bundle bundle=intent.getextras(); /*取出intent中附加的数据*/ string phone=bundle.getstring("phone"); string paswd=bundle.getstring("paswd"); string sex=bundle.getstring("sex"); string hobby=bundle.getstring("hobby"); string city=bundle.getstring("city");
界面显示如下:
实现如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/img03" tools:context="com.example.jinjin.applicationtest4.mainactivity"> <!--手机号--> <linearlayout android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absolutey="0dp" android:orientation="horizontal" tools:layout_editor_absolutex="8dp"> <textview android:layout_width="wrap_content" android:layout_height="40dp" android:textsize="18sp" android:textcolor="@android:color/background_dark" android:text="手机号:"/> <edittext android:id="@+id/phone" android:layout_width="match_parent" android:layout_height="50dp" android:inputtype="phone" android:hint="请输入手机号"/> </linearlayout> <!--密码--> <linearlayout android:layout_width="368dp" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="40dp" android:textsize="18sp" android:textcolor="@android:color/background_dark" android:text="密 码:"/> <edittext android:id="@+id/paswd" android:layout_width="match_parent" android:layout_height="50dp" android:inputtype="numberpassword" android:hint="请输入密码"/> </linearlayout> <!--性别选择--> <radiogroup android:id="@+id/sex" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <radiobutton android:id="@+id/nan" android:layout_width="50dp" android:layout_height="wrap_content" android:checked="true" android:text="男"/> <radiobutton android:id="@+id/nv" android:layout_width="50dp" android:layout_height="wrap_content" android:text="女"/> </radiogroup> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <checkbox android:id="@+id/read_book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读书" /> <checkbox android:id="@+id/play_ball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打球" /> <checkbox android:id="@+id/music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="听音乐" /> </linearlayout> <spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/register" android:layout_width="fill_parent" android:background="#3f51b5" android:textcolor="#ffffff" android:textsize="18sp" android:text="注 册" android:layout_height="40dp" /> </linearlayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@drawable/img03" android:layout_height="match_parent"> <textview android:id="@+id/show_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textsize="17sp" android:layout_marginleft="50dp" android:textcolor="@android:color/black" android:text="textview" /> </linearlayout>
mainactivity.java
package com.example.jinjin.applicationtest4; import android.content.intent; import android.os.bundle; import android.support.annotation.idres; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.adapterview; 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; public class mainactivity extends appcompatactivity implements view.onclicklistener,radiogroup.oncheckedchangelistener{ //定义字符串用来保存各个信息 private string phone_str=""; private string paswd_str=""; private string sex_str="男"; private string hobby_str="1"; private string city_str=""; //组件定义 edittext phone_edit,paswd_edit; radiogroup sex_group; radiobutton nan_but,nv_but; checkbox play,read,music; button register; spinner spinner; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //初始化组件 phone_edit = (edittext) findviewbyid(r.id.phone); paswd_edit=(edittext)findviewbyid(r.id.paswd); sex_group=(radiogroup)findviewbyid(r.id.sex); //添加监听事件 nan_but=(radiobutton)findviewbyid(r.id.nan); sex_group.setoncheckedchangelistener(this); read=(checkbox)findviewbyid(r.id.read_book); play=(checkbox)findviewbyid(r.id.play_ball); music=(checkbox)findviewbyid(r.id.music); register=(button)findviewbyid(r.id.register); //添加监听事件 register.setonclicklistener(this); spinner=(spinner)findviewbyid(r.id.spinner); // 创建arrayadapter对象 final string[] city=new string[]{"南宁","桂林","百色","柳州","玉林","河池"}; arrayadapter<string> adapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,city); spinner.setadapter(adapter); //城市下拉单列表添加监听事件 spinner.setonitemselectedlistener(new spinner.onitemselectedlistener(){ @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { city_str=city[i]; } @override public void onnothingselected(adapterview<?> adapterview) { //未选中状态 } }); } @override public void onclick(view view) { switch (view.getid()){ case r.id.register: //获取手机号和密码 phone_str=phone_edit.gettext().tostring(); paswd_str=paswd_edit.gettext().tostring(); //获取兴趣爱好即复选框的值 hobby_str="";//清除上一次已经选中的选项 if (read.ischecked()){ hobby_str+=read.gettext().tostring(); }if(play.ischecked()){ hobby_str+=play.gettext().tostring(); }if(music.ischecked()){ hobby_str+=music.gettext().tostring(); } intent intent=new intent(this,secondactivity.class); bundle bundle=new bundle(); bundle.putstring("phone",phone_str); bundle.putstring("paswd",paswd_str); bundle.putstring("sex",sex_str); bundle.putstring("hobby",hobby_str); bundle.putstring("city",city_str); intent.putextras(bundle); startactivity(intent); break; } } @override public void oncheckedchanged(radiogroup radiogroup, @idres int i) { //根据用户选择来改变sex_str的值 sex_str=i==r.id.nan?"男性":"女性"; } }
secondactivity.java
package com.example.jinjin.applicationtest4; import android.content.intent; import android.os.bundle; import android.support.annotation.nullable; import android.support.v7.app.appcompatactivity; import android.widget.textview; /** * created by jinjin on 2020/5/23. */ public class secondactivity extends appcompatactivity { @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); intent intent=this.getintent(); bundle bundle=intent.getextras(); string phone=bundle.getstring("phone"); string paswd=bundle.getstring("paswd"); string sex=bundle.getstring("sex"); string hobby=bundle.getstring("hobby"); string city=bundle.getstring("city"); textview show_text=(textview)findviewbyid(r.id.show_content); show_text.settext("手机号为:"+phone+"\n"+"密码为:"+paswd+"\n"+"性别是:"+sex+"\n"+"爱好是:"+hobby+"\n"+"城市是:"+city); } }
巩固监听事件:如果要对register和spinne的监听事件改造方法,如何重新实现监听?
- register可使用内部类,并重写onclick()方法 。
- spinner可使用实现接口的监听事件。
实现如下
package com.example.jinjin.applicationtest4; import android.content.intent; import android.os.bundle; import android.support.annotation.idres; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.adapterview; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.spinner; public class mainactivity extends appcompatactivity implements radiogroup.oncheckedchangelistener,adapterview.onitemselectedlistener{ //定义字符串用来保存各个信息 private string phone_str = ""; private string paswd_str = ""; private string sex_str = "男"; private string hobby_str = "1"; private string city_str = ""; //组件定义 edittext phone_edit, paswd_edit; radiogroup sex_group; radiobutton nan_but, nv_but; checkbox play, read, music; button register; spinner spinner; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //初始化组件 phone_edit = (edittext) findviewbyid(r.id.phone); paswd_edit = (edittext) findviewbyid(r.id.paswd); sex_group = (radiogroup) findviewbyid(r.id.sex); //添加监听事件 nan_but = (radiobutton) findviewbyid(r.id.nan); sex_group.setoncheckedchangelistener(this); read = (checkbox) findviewbyid(r.id.read_book); play = (checkbox) findviewbyid(r.id.play_ball); music = (checkbox) findviewbyid(r.id.music); register = (button) findviewbyid(r.id.register); //添加监听事件 // register.setonclicklistener(this); spinner = (spinner) findviewbyid(r.id.spinner); //直接new一个内部类对象作为参数 register.setonclicklistener(new mclick()); // final string[] city=new string[]{"南宁","桂林","百色","柳州","玉林","河池"}; // arrayadapter<string> adapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,city); // spinner.setadapter(adapter); // //城市下拉单列表添加监听事件 // spinner.setonitemselectedlistener(new spinner.onitemselectedlistener(){ // @override // public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { // city_str=city[i]; // } // @override // public void onnothingselected(adapterview<?> adapterview) { // } // }); //spinner加载监听事件 spinner.setonitemselectedlistener(this); } @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { city_str=mainactivity.this.getresources().getstringarray(r.array.city)[i]; } @override public void onnothingselected(adapterview<?> adapterview) { } //定义一个内部类,实现view.onclicklistener接口,并重写onclick()方法 class mclick implements view.onclicklistener { @override public void onclick(view view) { switch (view.getid()) { case r.id.register: //获取手机号和密码 phone_str = phone_edit.gettext().tostring(); paswd_str = paswd_edit.gettext().tostring(); //获取兴趣爱好即复选框的值 hobby_str = "";//清除上一次已经选中的选项 if (read.ischecked()) { hobby_str += read.gettext().tostring(); } if (play.ischecked()) { hobby_str += play.gettext().tostring(); } if (music.ischecked()) { hobby_str += music.gettext().tostring(); } intent intent = new intent(mainactivity.this, secondactivity.class); bundle bundle = new bundle(); bundle.putstring("phone", phone_str); bundle.putstring("paswd", paswd_str); bundle.putstring("sex", sex_str); bundle.putstring("hobby", hobby_str); bundle.putstring("city", city_str); intent.putextras(bundle); startactivity(intent); break; } } } @override public void oncheckedchanged(radiogroup radiogroup, @idres int i) { //根据用户选择来改变sex_str的值 sex_str = i == r.id.nan ? "男性" : "女性"; } }
在res/values下编写一个:array.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="city"> <item>南宁</item> <item>桂林</item> <item>柳州</item> <item>百色</item> <item>河池</item> <item>玉林</item> </string-array> </resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。