1. Post-run interface diagram
2. Main code:
2.1 Activity_main.xml (2 TextView 2 EditText 1 checkboxes and 1 buttons):
1<TextView2Android:id= "@+id/tvaccount"3Android:layout_width= "Wrap_content"4android:layout_height= "Wrap_content"5Android:layout_alignbaseline= "@+id/etaccount"6Android:layout_alignbottom= "@+id/etaccount"7android:text= "@string/tvaccount"8Android:textappearance= "? Android:attr/textappearancelarge"/>9 Ten<EditText OneAndroid:id= "@+id/etaccount" AAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -Android:layout_alignparentright= "true" theAndroid:layout_alignparenttop= "true" -android:layout_margintop= "18DP" -android:ems= "Ten"/> - +<EditText -Android:id= "@+id/etpass" +Android:layout_width= "Wrap_content" Aandroid:layout_height= "Wrap_content" atAndroid:layout_alignbottom= "@+id/tvpass" -android:layout_alignleft= "@+id/etaccount" -Android:ems= "10" -Android:inputtype= "Textpassword" > - -<requestfocus/> in</EditText> - to<TextView +Android:id= "@+id/tvpass" -Android:layout_width= "Wrap_content" theandroid:layout_height= "Wrap_content" *android:layout_alignleft= "@+id/tvaccount" $android:layout_below= "@+id/etaccount"Panax Notoginsengandroid:layout_margintop= "29DP" -android:text= "@string/tvpass" theAndroid:textappearance= "? Android:attr/textappearancelarge"/> + A<CheckBox theAndroid:id= "@+id/cbrempass" +Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" $android:layout_alignleft= "@+id/tvpass" $android:layout_below= "@+id/etpass" -android:layout_margintop= "20DP" -android:text= "@string/cbrempass"/> the -<ButtonWuyiAndroid:id= "@+id/btnlogin" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" WuAndroid:layout_alignbottom= "@+id/cbrempass" -android:layout_alignright= "@+id/etpass" Aboutandroid:text= "@string/btnlogin"/>
Activity_main.xml
2.2 Mainactivity.java
The main use of sharedpreferences to save the password
2.2.1 Definition Sharedpreferences
Private null;
2.2.2 Button click-to-Listen event
1 Private voidSetlistener () {2 //TODO auto-generated Method Stub3Btnlogin.setonclicklistener (NewOnclicklistener () {4 Public voidOnClick (View v) {5 //TODO auto-generated Method Stub6 //Determines whether the check box is selected7 if(cbrempass.ischecked ()) {8Msettings =getsharedpreferences (Prefs_name, mode_private);9Editor edit =Msettings.edit ();Ten //Mark OneEdit.putboolean ("Iskeep",true); A //Record User name -Edit.putstring ("username", Etaccount.gettext (). toString ()); -Edit.putstring ("Password", Etpass.gettext (). toString ()); the edit.commit (); -}Else{ -Msettings =getsharedpreferences (Prefs_name, mode_private); -Editor edit =Msettings.edit (); + //Mark -Edit.putboolean ("Iskeep",true); + //Record User name AEdit.putstring ("username", "" "); atEdit.putstring ("Password", "" "); - edit.commit (); - } - } - }); -}Listener Events for button buttons
2.2.3 The second time the key code to get the saved password is started
1 Private voidGetData () {2 //TODO auto-generated Method Stub3Msettings =getsharedpreferences (Prefs_name, mode_private);4 if(Msettings.getboolean ("Iskeep",false)){5Etaccount.settext (msettings.getstring ("username", "" "));6Etpass.settext (msettings.getstring ("Password", "" "));7}Else{8Etaccount.settext ("");9Etpass.settext ("");Ten } One}get the key code for the password
2.2.4 a reference to the GetData function is placed in the OnCreate method and Onresume method
3. Demo
Https://github.com/cnfanhua/A-RemPass
Android ' Remember password ' feature