In Android applications more and more include the network interactive function, this brings the registration, login account function. This article Complete Introduction dialog box way realizes the user login function.
Landing effect: Application to determine the current user has not logged in, pop-up login dialog box, user input account and password information, upload to the server verification, successful verification, the reality toast success information, and go to other interfaces.
Registration effect: If the user does not have an account, then click on the Login dialog box "No account, fast registration account", pop-up registration interface, user input registration information, click the Registration button, registered successful, pop-up toast information "registration success", complete registration, go to other functional interface.
The whole function is divided into two parts: Login dialog box: Enter the login information, realize the login function, go to the registration interface. Registration dialog box: Enter the registration information, to achieve the registration function.
dialog box interface layout XML file:
<?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=" wrap_content "android:orientation=" vertical " > <textview android:id= "@+id/txt_loginerror" android:layout_height= "Wrap_content" android:layout_width
= "Wrap_content" android:layout_marginleft= "20dip" android:layout_marginright= "20dip" android:textcolor= "#ff0000"
Android:text= "entered the account and password is incorrect" android:gravity= "left" android:textappearance= "? Android:attr/textappearancemedium" android:visibility= "Invisible"/> <textview android:id= "@+id/username" android:layout_height=
Content "Android:layout_width=" wrap_content "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "
android:text= "Account" android:gravity= "left" android:textappearance= "? Android:attr/textappearancemedium"/> <edittext android:id= "@+id/Txt_username "android:layout_height=" wrap_content "android:layout_width=" Fill_parent "android:layout_marginLeft=" 20dip "android:layout_marginright=" 20dip "android:autotext=" false "android:capitalize=" None "android:gravity=" Fill_horizontal "android:textappearance="? Android:attr/textappearancemedium "/> <TextView android:id=" @+ Id/password "android:layout_height=" wrap_content "android:layout_width=" Wrap_content "android:layout_marginLeft=" 20dip "android:layout_marginright=" 20dip "android:textappearance=" Android:attr/textappearancemedium "Android:te xt= "password" android:gravity= "left"/> <edittext android:id= "@+id/txt_password" android:layout_height= "wrap"
_content "android:layout_width=" fill_parent "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip " Android:autotext= "false" android:capitalize= "None" android:gravity= "Fill_horizontal" android:textappearance= " ? android:attr/textappearAncemedium "/> <textview android:id= @+id/txt_toregister" android:layout_height= "Wrap_content" Android:layout_width= "Wrap_content" android:layout_marginleft= "20dip" android:layout_marginright= "20dip" Android: Textcolor= "#2200C1 android:textappearance="? Android:attr/textappearancemedium "android:text=" no account? Fast Registration "Android
: gravity= "left"/> </LinearLayout>
Background business logic:
* * Create User Login dialog box * Login interface contains two buttons * 1 button for landing * 2 button for no login demo */private void Createloginalert () {Alertdialog.bu
Ilder ad =new Alertdialog.builder (this);
Ad.settitle ("account login");
Ad.setview (Viewutility.getview (This,r.layout.sub_logindialog));
Adi= ad.create (); /*/Adi.setbutton ("Login", new Onclicklistener () {@Override public void OnClick (Dialoginterface arg0
, int arg1) {edittext password= (edittext) Adi.findviewbyid (R.id.txt_password);
EditText account = (edittext) Adi.findviewbyid (r.id.txt_username);
Password=password.gettext (). toString ();
Account=account.gettext (). toString ();
Generate Login dialog box M_dialog=progressdialog.show (main.this, "Please wait ...", "is landing for you ...", true);
Mredrawhandler.sleep (100);
}
});
Adi.setbutton2 ("Demo", New Onclicklistener () {@Override public void OnClick (dialoginterface arg0, int arg1) { Viewutility.navigateactivate (MAin.this, Selecttheme.class);
}
});
Adi.show ();
Set up the registration click event TextView register= (TextView) Adi.findviewbyid (r.id.txt_toregister);
Register.setonclicklistener (New Textview.onclicklistener () {public void OnClick (View v) {///Create Registration dialog box
Createregisteralert ();
Adi.dismiss ();
}
});
*/* * timed thread to do verification with */private Refreshhandler Mredrawhandler = new Refreshhandler ();
Class Refreshhandler extends Handler {@Override public void Handlemessage (msg) {try{
Call the network interface to implement the login command Boolean flags= userdataservicehelper.login (account, PassWord);
if (flags) {//Save login Information Userdatawritehelper uw=new userdatawritehelper (main.this); Uw.
Saveuserinfoindb ("xuwenbing", account);
Prompt landing Success Toast.maketext (Main.this, "landing success", Toast.length_short). Show (); Go to Theme page Viewutility.navigateactivate (main.this, selecttheme.class);
}else {///failure displays error message Toast.maketext (main.this, "Login Failed", Toast.length_short). Show ();
Adi.show ();
Adi.findviewbyid (R.id.txt_loginerror). setvisibility (view.visible);
} catch (Exception e) {e.printstacktrace ();
} finally{M_dialog.dismiss ();
} public void sleep (long delaymillis) {this.removemessages (0);
Sendmessagedelayed (obtainmessage (0), delaymillis);
}
};
Dialog box interface layout XML file:
<?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=" wrap_content "android:orientation=" vertical " > <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:orientati on= "Horizontal" android:gravity= "center" > <textview android:id= "@+id/txt_loginerror" Android:layout_hei ght= "Wrap_content" android:layout_width= "wrap_content" android:layout_marginleft= "20dip" Android:layout_marginRig ht= "20dip" "android:textcolor=" #ff0000 "android:text=" entered the account and password is incorrect "android:gravity= left" Android:textappearanc E= "Android:attr/textappearancemedium" android:visibility= "invisible"/> </LinearLayout> <linearl
Ayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <textview android:id= "@+Id/username "android:layout_height=" wrap_content "android:layout_width=" Wrap_content "android:layout_marginLeft=" 20dip "android:layout_marginright=" 20dip "android:text=" account "android:gravity=" left "android:textappearance="? Droid:attr/textappearancemedium "/> <edittext android:id=" @+id/txt_username "android:layout_height=" W Rap_content "android:layout_width=" fill_parent "android:layout_marginleft=" 20dip "android:layout_marginright=" 20d IP "android:autotext=" false "android:capitalize=" None "android:gravity=" Fill_horizontal "Android:textappearanc E= "Android:attr/textappearancemedium"/> </LinearLayout> <linearlayout android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" > <TextView Androi D:id= "@+id/password" android:layout_height= "wrap_content" android:layout_width= "Wrap_content" Android:layout_marg inleft= "20dip" Android:layout_marginright= "20dip" android:textappearance= "android:attr/textappearancemedium" android:text= "password" Andro Id:gravity= "left"/> <edittext android:id= "@+id/txt_password" android:layout_height= "Wrap_content" and Roid:layout_width= "Fill_parent" android:layout_marginleft= "20dip" android:layout_marginright= "20dip" Android:auto Text= "false" android:capitalize= "None" android:gravity= "Fill_horizontal" android:textappearance= "? Android:attr/te Xtappearancemedium "/> </LinearLayout> <linearlayout android:layout_width=" fill_parent "Andro"
id:layout_height= "wrap_content" android:orientation= "horizontal" > <textview android:id= "@+id/nicename" android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:layout_marginleft= "20dip" and roid:layout_marginright= "20dip" android:textappearance= "Android:attr/textappearancemedium" android:text= "nickname" an
Droid:gravity= "left"/> <edittext android:id= "@+id/txt_nicename" android:layout_height= "wrap_content" android:layout_width= "fi Ll_parent "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "android:autotext=" false "Andro Id:capitalize= "None" android:gravity= "Fill_horizontal" android:textappearance= "? android:attr/
Textappearancemedium "/> </LinearLayout> </LinearLayout>
Background business logic:
/* Create Registration dialog box */private void Createregisteralert () {//registerdialog alertdialog.builder ad =new Alertdialog.bui
Lder (this);
Ad.settitle ("registered account");
Ad.setview (Viewutility.getview (This,r.layout.sub_registerdialog));
Registerdialog= ad.create (); Registerdialog.setbutton ("register", new Onclicklistener () {@Override public void OnClick (dialoginterface arg0, int
ARG1) {EditText password= (edittext) Registerdialog.findviewbyid (R.id.txt_password);
EditText account = (edittext) Registerdialog.findviewbyid (r.id.txt_username);
EditText nicename = (edittext) Registerdialog.findviewbyid (r.id.txt_nicename);
Password=password.gettext (). toString ();
Account=account.gettext (). toString ();
Nicename=nicename.gettext (). toString ();
Generate Registration dialog box M_dialog=progressdialog.show (main.this, "Please wait ...", "is registering for you ...", true);
Mregsiterhandler.sleep (100);
}
}); Registerdialog.setbutton2 ("Demo", New Onclicklistener () {@Override public void OnClick (dialoginterface arg0, int arg1) {Viewut Ility.
Navigateactivate (Main.this, Selecttheme.class);
}
});
Registerdialog.show ();
* * * * The Timer registration process */private Regsiterhandler Mregsiterhandler = new Regsiterhandler ();
Class Regsiterhandler extends Handler {@Override public void Handlemessage (msg) {try{
Calls the network interface, implements the registration instruction Boolean flags= userdataservicehelper.register (account, password,nicename);
if (flags) {//Save registration Information Userdatawritehelper uw=new userdatawritehelper (main.this); Uw.
Saveuserinfoindb ("xuwenbing", account);
Prompt to register successfully toast.maketext (Main.this, "registration successful", Toast.length_short). Show ();
Go to Theme page viewutility.navigateactivate (Main.this, Selecttheme.class); }else {//failure displays error message Toast.maketext (Main.this, "registration failed", toast.length_sHort). Show ();
Registerdialog.show ();
Registerdialog.findviewbyid (R.id.txt_loginerror). setvisibility (view.visible);
} catch (Exception e) {e.printstacktrace ();
} finally{M_dialog.dismiss ();
} public void sleep (long delaymillis) {this.removemessages (0);
Sendmessagedelayed (obtainmessage (0), delaymillis);
}
};
Two network interface functions:
Call the network interface to implement the login command
Boolean flags= userdataservicehelper.login (account, PassWord);
Calls the network interface, implements the registration instruction
Boolean flags= userdataservicehelper.register (account, password,nicename);