java实现简单注册选择所在城市
程序员文章站
2024-03-09 12:57:27
本文实例为大家分享了java实现简单注册选择所在城市的全部代码,供大家参考,具体内容如下
1.activity_main.xml &nb...
本文实例为大家分享了java实现简单注册选择所在城市的全部代码,供大家参考,具体内容如下
1.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="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <edittext android:id="@+id/user" android:minwidth="200px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <linearlayout android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:" /> <radiogroup android:id="@+id/sex" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <radiobutton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男"/> <radiobutton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </radiogroup> </linearlayout> <linearlayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/textview1" android:text="请选择所在城市:" android:layout_height="wrap_content" android:layout_width="wrap_content"/> <spinner android:entries="@array/ctype" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/spinner1"/> </linearlayout> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:"/> <edittext android:id="@+id/pwd" android:minwidth="200px" android:inputtype="textpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认密码:" /> <edittext android:id="@+id/repwd" android:minwidth="200px" android:inputtype="textpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="e-mail地址:" /> <edittext android:id="@+id/email" android:minwidth="400px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </linearlayout>
2.register.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" > <textview android:id="@+id/user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="用户名:" /> <textview android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="性别:" /> <textview android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="城市:" /> <textview android:id="@+id/pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="密码:" /> <textview android:id="@+id/email" android:padding="10px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="e-mail:" /> <button android:id="@+id/back" android:text="返回上一步" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
3. mainactivity.java
package com.example.ejcker_llin.myapplication; import android.app.activity; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.button; 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 button submit; private string sex1; private string city; final int code=0x717; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); submit= (button) findviewbyid(r.id.submit); submit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string user=((edittext)findviewbyid(r.id.user)).gettext().tostring(); string pwd=((edittext)findviewbyid(r.id.pwd)).gettext().tostring(); string repwd=((edittext)findviewbyid(r.id.repwd)).gettext().tostring(); string email=((edittext)findviewbyid(r.id.email)).gettext().tostring(); radiogroup sex= (radiogroup) findviewbyid(r.id.sex); for(int i=0;i<sex.getchildcount();i++){ radiobutton r= (radiobutton) sex.getchildat(i); if(r.ischecked()){ sex1=r.gettext().tostring(); break; } } spinner spinner= (spinner) findviewbyid(r.id.spinner1); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { city=parent.getitematposition(position).tostring(); } @override public void onnothingselected(adapterview<?> parent) { } }); if(!"".equals(user)&&!"".equals(pwd)&&!"".equals(email)){ if(!pwd.equals(repwd)){ toast.maketext(mainactivity.this,"两次输入的密码不一致,请重新输入!",toast.length_long).show(); ((edittext) findviewbyid(r.id.pwd)).settext(""); ((edittext) findviewbyid(r.id.repwd)).settext(""); ((edittext) findviewbyid(r.id.pwd)).requestfocus(); }else { intent intent=new intent(mainactivity.this,registeracivity.class); bundle bundle=new bundle(); bundle.putcharsequence("user",user); bundle.putcharsequence("sex",sex1); bundle.putcharsequence("city",city); bundle.putcharsequence("pwd",pwd); bundle.putcharsequence("email",email); intent.putextras(bundle); //startactivity(intent); startactivityforresult(intent,code); } }else { toast.maketext(mainactivity.this,"请将注册信息输入完整!",toast.length_long).show(); } } }); } }
4. registeracivity.java
package com.example.ejcker_llin.myapplication; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; /** * created by jcker_llin on 2016/4/5. */ public class registeracivity extends activity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register); final intent intent=getintent(); bundle bundle=intent.getextras(); textview user= (textview) findviewbyid(r.id.user); user.settext("用户名:"+bundle.getstring("user")); textview sex= (textview) findviewbyid(r.id.sex); sex.settext("性别:"+bundle.getstring("sex")); textview city= (textview) findviewbyid(r.id.city); city.settext("城市:"+bundle.getstring("city")); textview pwd= (textview) findviewbyid(r.id.pwd); pwd.settext("密码:"+bundle.getstring("pwd")); textview email= (textview) findviewbyid(r.id.email); email.settext("e-mail:"+bundle.getstring("email")); button button= (button) findviewbyid(r.id.back); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setresult(0x717,intent); finish(); } }); } }
5.
6.
7. arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="ctype"> <item>北京</item> <item>上海</item> <item>广州</item> <item>杭州</item> <item>天津</item> <item>香港</item> <item>重庆</item> <item>西安</item> <item>其他</item> </string-array> </resources>
以上就是本文的全部内容,希望对大家的学习有所帮助。
下一篇: Java对数组实现选择排序算法的实例详解