Registration page:
User_register.xml:
<?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 " android:background= "@drawable/bg_01" > "<textview android:layout_width=" wrap_content "Androi" d:layout_height= "Wrap_content" android:text= "register" android:textsize= "22dip" android:textcolor= "#FFFFFF" android:paddingleft= "140dip" android:paddingright= "50dip" android:paddingtop= "10dip" Android: background= "@drawable/topbg"/> "<edittext android:id=" @+id/register_username "Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "20dip" and roid:background= "@drawable/search" android:layout_marginleft= "20dip" android:layout_marginright= "20dip" Android:height= "40dip" android:hint= "user name"/> <edittext android:id= "@+id/register_passwd" android:layout_width= "Wra P_content "android:layout_height=" wrap_content "android:layout_margintop=" 20dip "android:background=" @drawable/search "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "Android:heigh t= "40dip" android:hint= "password"/> <edittext android:id= "@+id/reregister_passwd" a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "20dip" android:background= "@drawable/search" android:layout_marginleft= "20dip" android:layout_marginright= "20d IP "android:height=" 40dip "android:hint=" Confirm password "/> <button android:id=" @+id/regis Ter_submit "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:ba ckground= "@drawable/TOPBG "android:height=" 40dip "android:width=" 70dip "android:layout_margintop=" 60dip "Andro id:text= "OK" android:textcolor= "#FFFFFF" android:textsize= "22dip"/> </linearla Yout>
Handling activity on the registration page:
Package Com.example.foreveross.office;import Java.io.ioexception;import java.io.UnsupportedEncodingException; Import Java.util.arraylist;import Java.util.list;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.parseexception;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.util.entityutils;import Com.example.wenandroid.r;import Android.app.activity;import Android.os.bundle;import Android.os.strictmode;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Userregister extends Activity {private EditText register_username;private EditText register_passwd;private EditText reregister_passwd;private Button register_submit; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Strictmode.threadpolicy policy = new StrictMode.ThreadPolicy.Builder (). Permitall (). build (); Strictmode.setthreadpolicy (policy); Setcontentview (R.layout.user_register); Register_username= (EditText) Findviewbyid (R.id.register_username); register_passwd= (EditText) Findviewbyid (R.ID.REGISTER_PASSWD); reregister_ Passwd= (EditText) Findviewbyid (R.ID.REREGISTER_PASSWD); register_submit= (Button) Findviewbyid (r.id.register_ Submit); Register_username.setonfocuschangelistener (new Onfocuschangelistener () {@Overridepublic void Onfocuschange (View V, boolean hasfocus) {//TODO auto-generated method stubif (!hasfocus) {if (Register_username.gettext (). toString (). Trim (). Length () <4) { Toast.maketext (userregister.this, "User name cannot be less than 4 characters", Toast.length_short). show ();}}}); Register_passwd.setonfocuschangelistener (New Onfocuschangelistener () {@Overridepublic void Onfocuschange (View V, Boolean Hasfocus) {//TODO auto-generated method stubif (!hasfocus) {if (Register_passwd.gettext (). toString (). Trim (). Length () <6) {Toast.maketext (userregister.this, "password cannot be less than 8 characters", Toast.length_short). Show ();}}}); Reregister_passwd.setonfocuschangelistener (New Onfocuschangelistener () {@Overridepublic void Onfocuschange (View V, Boolean Hasfocus) {//TODO auto-generated method stubif (!hasfocus) {if (!reregister_passwd.gettext (). toString (). Trim () . Equals (Register_passwd.gettext (). toString (). Trim ())) {Toast.maketext (userregister.this, "two times password input inconsistent", Toast.length_short). Show (); }}}); Register_submit.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {if (!checkedit ( ) {return;} TODO auto-generated method stubstring httpurl= "http://192.168.1.100:8080/web-test/register.jsp"; HttpPost httprequest=new HttpPost (Httpurl); List<namevaluepair> params=new ArRaylist<namevaluepair> ();p Arams.add (New Basicnamevaluepair ("username", Register_username.gettext (). ToString (). Trim ());p Arams.add (New Basicnamevaluepair ("Password", Register_passwd.gettext (). ToString (). Trim ())); Httpentity httpentity = null;try {httpentity = new urlencodedformentity (params, "UTF8");} catch ( Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Httprequest.setentity (httpentity); HttpClient httpclient=new defaulthttpclient (); HttpResponse HttpResponse = null;try {HttpResponse = Httpclient.execute (HttpRequest);} catch (Clientprotocolexception E {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} if (Httpresponse.getstatusline (). Getstatuscode () ==200) {String strresult = null;try {strresult = entityutils.tostring ( Httpresponse.getentity ());} catch (ParseException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-genErated catch Blocke.printstacktrace ();} Toast.maketext (Userregister.this, Strresult, Toast.length_short). Show (); Else{toast.maketext (userregister.this, "Request Error", Toast.length_short). Show ();}});} Private Boolean Checkedit () {if (Register_username.gettext (). toString (). Trim (). Equals ("")) {Toast.maketext ( Userregister.this, "User name cannot be empty", Toast.length_short). Show (); else if (Register_passwd.gettext (). toString (). Trim (). Equals ("")) {Toast.maketext (userregister.this, "password cannot be null", Toast.length_short). Show ();} else if (!register_passwd.gettext (). ToString (). Trim (). Equals (Reregister_passwd.gettext (). ToString (). Trim ())) { Toast.maketext (Userregister.this, "two times password input inconsistency", toast.length_short). Show (); Else{return true;} return false;}}
Login Page xml:
User_login.xml:
<?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 " android:background= "@drawable/bg_01" > <textview android:layout_width= "wrap_content" Android: layout_height= "wrap_content" android:text= "Login" android:textsize= "22dip" android:textcolor= "#FFFFFF" android:paddingleft= "140dip" android:paddingright= "50dip" android:paddingtop= "10dip" Android:ba ckground= "@drawable/topbg"/> <linearlayout android:layout_width= "Fill_parent" android:layout_he ight= "wrap_content" android:orientation= "vertical" > <edittext android:id= "@+id/login_usern Ame "Android:layout_width=" fill_parent "android:layout_height=" 40dip "android:layout_marginleft=" 20di P "Android:layout_marginright= "20dip" android:layout_margintop= "30dip" android:hint= "User name" android:paddingtop= "10dip" Andro Id:textsize= "18dip" android:background= "@drawable/search" > </EditText> & Lt EditText android:id= "@+id/login_password" android:layout_width= "Fill_parent" android:layout_height= "4 0dip "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "android:layout_margintop=" 10dip "android:password=" true "android:paddingtop=" 10dip "android:textsize=" 18dip "Android:hin t= "Password" android:background= "@drawable/search" > </EditText> </LinearLayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:layout_gravity= " Center_horizontal "android:layout_margintop=" 15dip "> <checkbox android:id=" @+id/cb1 " Android:layout_wiDth= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "50dip" android:layout_marginright= "30dip" android:text= "Remember Password" android:button= "@drawable/checkbox_ico N_no "/>" <checkbox android:id= "@+id/cb2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Automatic Login" android:paddingright= "50dip" android:button= "@drawable/checkbox_icon_no"/> </LinearLayout> <linearlayout Andro Id:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" and roid:layout_margintop= "20dip" > <button android:id= "@+id/user_login_button" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Login" and Roid:layout_marginleft= "50dip "android:textcolor=" #F7FBFD "android:background=" #FF0000 "android:width=" 70dip " android:height= "40dip" android:textsize= "18dip"/> <button Android:id= "@+id/user_register_button" android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:text= "registered" android:layout_marginleft= "50dip" Android: Textcolor= "#F7FBFD" android:width= "70dip" android:height= "40dip" android:background= "# 0f9000 "android:textsize=" 18dip "/> </LinearLayout> </linearlayout> ;
Login page Activity:
Package Com.example.foreveross.office;import Java.io.ioexception;import java.io.UnsupportedEncodingException; Import Java.util.arraylist;import Java.util.list;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.parseexception;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.util.entityutils;import Com.example.wenandroid.r;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.os.strictmode;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.Button; Import Android.widget.edittext;import Android.widget.toast;public clUserlogin extends Activity implements Onclicklistener {private EditText login_username;private EditText login_ Password;private button User_login_button;private button user_register_button; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Strictmode.threadpolicy policy = new StrictMode.ThreadPolicy.Builder (). Permitall (). build (); Strictmode.setthreadpolicy (policy); Setcontentview (R.layout.user_login); Initwidget ();} private void Initwidget () {login_username= (EditText) Findviewbyid (r.id.login_username); login_password= (EditText) Findviewbyid (R.id.login_password); user_login_button= (Button) Findviewbyid (R.id.user_login_button); user_register _button= (Button) Findviewbyid (R.id.user_register_button); User_login_button.setonclicklistener (this); user_ Register_button.setonclicklistener (this); Login_username.setonfocuschangelistener (new Onfocuschangelistener () {@ overridepublic void Onfocuschange (View V, boolean hasfocus) {//TODO auto-generated method Stubif (!hasFocus) {String username=login_username.gettext (). toString (). Trim (); if (Username.length () <4) {Toast.maketext ( Userlogin.this, "User name cannot be less than 4 characters", Toast.length_short);}}}); Login_password.setonfocuschangelistener (New Onfocuschangelistener () {@Overridepublic void Onfocuschange (View V, Boolean Hasfocus) {//TODO auto-generated method stubif (!hasfocus) {String password=login_password.gettext (). ToString ( ). Trim (); if (Password.length () <4) {Toast.maketext (userlogin.this, "password cannot be less than 4 characters", Toast.length_short);}}});} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.id.user_login_button: if (Checkedit ()) {login ();} Break;case r.id.user_register_button:intent intent2=new Intent (userlogin.this,userregister.class); StartActivity ( INTENT2); break;}} Private Boolean Checkedit () {if (Login_username.gettext (). toString (). Trim (). Equals ("")) {Toast.maketext ( Userlogin.this, "User name cannot be empty", Toast.length_short). Show (); else if (Login_password.gettext (). toString (). Trim (). Equals ("")) {toast.maKetext (userlogin.this, "Password cannot be empty", Toast.length_short). Show (); Else{return true;} return false;} private void Login () {String httpurl= "http://192.168.1.102:8080/web-test/login.jsp"; HttpPost httprequest=new HttpPost (Httpurl); List<namevaluepair> params=new arraylist<namevaluepair> ();p Arams.add (New Basicnamevaluepair ("username ", Login_username.gettext (). toString (). Trim ()));p Arams.add (New Basicnamevaluepair (" Password ", Login_ Password.gettext (). toString (). Trim ())); Httpentity httpentity = null;try {httpentity = new urlencodedformentity (params, "UTF8");} catch ( Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Httprequest.setentity (httpentity); HttpClient httpclient=new defaulthttpclient (); HttpResponse HttpResponse = null;try {HttpResponse = Httpclient.execute (HttpRequest);} catch (Clientprotocolexception E {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} if (httprespOnse.getstatusline (). Getstatuscode () ==200) {String strresult = null;try {strresult = entityutils.tostring ( Httpresponse.getentity ());} catch (ParseException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();} Toast.maketext (Userlogin.this, Strresult, Toast.length_short). Show (); Intent intent=new Intent (Userlogin.this, Indexactivity.class); startactivity (intent);} Else{toast.maketext (userlogin.this, "Login failed! ", Toast.length_short). Show ();}}}
Login success Jump to Indexactivity.java
From: http://www.cnblogs.com/dyllove98/p/3235407.html
Android Registration, login implementation Program