XML file
<?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:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.example.dell.wodelianxi.denglucunchu"Android:background= "@drawable/beijing"android:orientation= "Vertical" > <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name"Android:id= "@+id/et_user"/> <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Numberpassword"Android:hint= "Please enter password"Android:id= "@+id/et_pwd"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <CheckBox android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Save user name, password"Android:layout_weight= "1"Android:id= "@+id/cb_jizhu"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Register"Android:layout_weight= "2"Android:background= "@drawable/zhuce"Android:id= "@+id/zhuce"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Login"Android:layout_weight= "2"Android:background= "@drawable/zhuce"Android:id= "@+id/denglu"Android:onclick= "Denglu"/> </LinearLayout></LinearLayout>
Java code
PackageCom.example.dell.wodelianxi;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classDenglucunchuextendsappcompatactivity {EditText et_user; EditText et_pwd; CheckBox Cb_jizhu; Sharedpreferences sp; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_denglucunchu); SP= getsharedpreferences ("info", mode_private); Et_user=(EditText) Findviewbyid (R.id.et_user); Et_pwd=(EditText) Findviewbyid (R.ID.ET_PWD); Cb_jizhu=(CheckBox) Findviewbyid (R.id.cb_jizhu); Et_user.settext (Sp.getstring ("User",NULL)); Et_pwd.settext (Sp.getstring ("Password",NULL)); } Public voidDenglu (view view) {if(cb_jizhu.ischecked ()) {Sharedpreferences.editor Editor=Sp.edit (); String User=Et_user.gettext (). toString (); String Password=Et_pwd.gettext (). toString (); if(Textutils.isempty (user) | |textutils.isempty (password)) {Toast.maketext (Denglucunchu). This, "The user name or password cannot be empty", Toast.length_short). Show (); } Else{editor.putstring ("User", user); Editor.putstring ("Password", password); //editor.putstring (User,password);Editor.commit (); Toast.maketext (Denglucunchu. This, "Saved successfully", Toast.length_short). Show (); } } Else{toast.maketext (Denglucunchu). This, "Please save user name, password", Toast.length_short). Show (); } }}
Login interface user name, password storage and re-login to automatically populate the user name, password