We use the general application has to remember password, automatic login function, so that users do not have to click the login button every time to enhance the user experience, below we have an example of how to implement the automatic login via Android sharedpreferenced. This example has three interface-login interface, jump interface, login successful interface.
1. Login screen:
<?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:background=" @drawable/log O_BG "android:orientation=" vertical "> <relativelayout android:layout_width=" fill_parent "Androi d:layout_height= "Wrap_content" > <imagebutton android:id= "@+id/img_btn" Android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:background= "@drawable/quit"/> <textview android:id= "@+id/tv_zh" Androi D:layout_width= "Wrap_content" android:layout_height= "35dip" android:layout_marginleft= "12dip" android:layout_margintop= "10dip" android:gravity= "Bottom" android:text= "Account:" Android : textcolor= "#000000" andRoid:textsize= "18sp"/> <edittext android:id= "@+id/et_zh" android:layout_width= "Fill_pa Rent "android:layout_height=" 40dip "android:layout_below=" @id/tv_zh "Android:layout_margi nleft= "12dip" android:layout_marginright= "10dip"/> <textview android:id= "@+id/tv_mima" Android:layout_width= "Wrap_content" android:layout_height= "35dip" android:layout_below= " @id/et_zh "android:layout_marginleft=" 12dip "android:layout_margintop=" 10dip "Android:gra vity= "Bottom" android:text= "Password:" android:textcolor= "#000000" android:textsize= "18SP"/> ; <edittext android:id= "@+id/et_mima" android:layout_width= "Fill_parent" Android:layout_ height= "40dip" android:layout_below= "@id/tv_mima" android:layout_marginleft= "12dip" Andro Id:layout_marginright= "10dip" android:maxlines= "$" android:password= "true" android:scrollhorizontally= "True"/> <checkbox android:id= "@+id/cb_auto" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@id/et_mima" Android:layout_marginl eft= "12dip" android:text= "Automatic Login" android:textcolor= "#000000"/> <button Andro Id:id= "@+id/btn_mima" android:layout_width= "100dip" android:layout_height= "40dip" Android : layout_below= "@id/cb_auto" android:layout_marginleft= "12dip" android:gravity= "center" an droid:text= "Forgot password" android:textcolor= "#000000" android:textsize= "18sp"/> <button Android:id= "@+id/btn_login" android:layout_width= "80dip" android:layout_height= "40dip" Android:layout_alignright= "@+id/et_mima" android:layout_below= "@+id/cb_auto" android:gravity= "Center" android: text= "Login" android:textcolor= "#000000" android:textsize= "18sp"/> </relativelayout></l Inearlayout>
2. Jump interface:
<?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:background=" @drawable/log O_BG "android:orientation=" vertical "> <relativelayout android:layout_width=" fill_parent "Androi d:layout_height= "Wrap_content" android:layout_weight= "3" > <progressbar android:id= "@+id/pg Bar "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:lay Out_centerinparent= "true"/> <textview android:id= "@+id/tv1" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:layout_below=" @id/pgbar "android:l Ayout_centerhorizontal= "true" android:text= "logging in ..." android:textcolor= "#000000" Android: Textsize= "18sp"/> </relativelayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:orientation= "vertical" > <butto n android:id= "@+id/btn_back" android:layout_width= "70dip" android:layout_height= "35dip" Android:text= "Cancel" android:textcolor= "#000000" android:textsize= "12sp"/> </linear Layout></linearlayout>
3. Successful interface:
<?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:layout_ gravity= "center" android:background= "@drawable/login_bg" android:orientation= "vertical" > < TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= " Center " android:text=" landed successfully, entered the user interface " android:textcolor=" #000000 " android:textsize=" 20SP "/></ Linearlayout>
Here's a look at 3 activity:
Loginactivity.java:
Package Com.liu.activity;import Android.app.activity;import Android.content.context;import android.content.Intent; Import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.Bundle ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.edittext;import Android.widget.imagebutton;import Android.widget.toast;public class Loginactivity extends Activity {private EditText UserName, password;private CheckBox auto_login;private Button btn_login, btn_forget_pass;private ImageButton btnquit; Private String Usernamevalue, passwordvalue;private sharedpreferences sp;public void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//Remove title This.requestwindowfeature (Window.FEATURE_NO_TITLE ); Setcontentview (r.layout.login);//Get Instance Object sp = This.getsharedpreferences ("UserInfo", context.mode_world_readable); userName = (EditText) Findviewbyid ( R.id.et_zh);p Assword = (EditText) Findviewbyid (R.id.et_mima); btn_forget_pass = (Button) Findviewbyid (R.id.btn_mima); Auto_login = (CheckBox) Findviewbyid (r.id.cb_auto), Btn_login = (Button) Findviewbyid (r.id.btn_login); btnquit = ( ImageButton) Findviewbyid (R.ID.IMG_BTN);//Judgment remember the State of the Password multi box if (Sp.getboolean ("Auto_ischeck", False)) {// Set default is record password status Username.settext (sp.getstring ("user_name", ""));p Assword.settext (sp.getstring ("Password", ""));} else if (Sp.getboolean ("Auto_ischeck", True)) {Intent Intent = new Intent (loginactivity.this,jumpactivity.class); LoginActivity.this.startActivity (Intent); LoginActivity.this.finish (); auto_login.setchecked (true);} The logon listener event is now defaulted to the user name: Yayun Password: 123btn_login.setonclicklistener (new Onclicklistener () {public void OnClick (View v) { Usernamevalue = Username.gettext (). ToString ();p asswordvalue = Password.gettext (). ToString (); if ( Usernamevalue.equals ("Yayun") && passwordvalue.equals ("123")) {Toast.maketext (loginactivity.this, "Login Successful", Toast.length_short). Show ();// The login success and remember Password box is selected to save the user information if (auto_login.ischecked ()) {//Remember user name, password, editor editor = Sp.edit (); Editor.putstring ("User_name ", Usernamevalue); Editor.putstring (" PASSWORD ", Passwordvalue); Editor.commit ();} Jump Interface Intent Intent = new Intent (Loginactivity.this,jumpactivity.class); LoginActivity.this.startActivity (Intent); LoginActivity.this.finish ();} else {toast.maketext (loginactivity.this, "User name or password is wrong, please login again", Toast.length_long). Show ();}}); /Listen Remember password Multi box button event Auto_login.setoncheckedchangelistener (new Oncheckedchangelistener () {public void oncheckedchanged ( Compoundbutton Buttonview,boolean isChecked) {if (auto_login.ischecked ()) {System.out.println ("Remember password is selected"); Sp.edit (). Putboolean ("Auto_ischeck", true). commit ();} else {System.out.println ("Remember Password not selected"); Sp.edit (). Putboolean ("Auto_ischeck", false). commit ();}}); Btnquit.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {LoginACtivity.this.finish ();}});}}
2.jumpactivity.java:
Package Com.liu.activity;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.os.handler;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.Window; Import Android.widget.button;import Android.widget.progressbar;public class Jumpactivity extends Activity {private ProgressBar progressbar;private Button backbutton;protected void OnCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate);//Remove title this.requestwindowfeature (window.feature_no_title); Setcontentview (R.layout.jump); ProgressBar = (ProgressBar) Findviewbyid (r.id.pgbar); Backbutton = (Button) Findviewbyid (r.id.btn_back); Startwelcomeavtivity (); Backbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) { JumpActivity.this.finish ();}});} private void Startwelcomeavtivity () {new Handler (). postdelayed (New Runnable () {//delay operation public void run () {Intent Intent = NE W Intent (Jumpactivity.this,welcomeavtivity.class); Jumpactivity. this.startactivity (Intent); JumpActivity.this.finish ();}},2000);//Set Execution time 2 sec}}
3.welcome.java:
Package Com.liu.activity;import Android.app.activity;import Android.os.bundle;public class Welcomeavtivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.welcome);}}
Running an instance:
This should be recorded in the password, you can automatically log in, we test, quit the application and enter again, as follows:
If you want to be more perfect, you can do a log out of the operation Oh!
Favorite friends pay attention to me!
SOURCE download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Combat Simple tutorial-66th gun (combined with sharedpreferenced for automatic login function)