When writing the Android login screen, how do we remember the password to facilitate the next login?
First, create an Android project (my version is 4.4.2) with the name "Remember Password"
Then find the Layout folder under the Res folder, find Activity_main.xml or fragment_main.xml, enter or drag the button
<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 "Android:orien tation= "vertical" tools:context= "com.csdn.www.MainActivity" > <textview android:id= "@+id/textview1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "user name"/> < ; EditText android:id= "@+id/username" android:layout_width= "fill_parent" android:layout_height= "Wrap_co Ntent "/> <textview android:id=" @+id/textview2 "android:layout_width=" Fill_parent "android:l ayout_height= "wrap_content" android:text= "password"/> <edittext android:id= "@+id/userpass" Androi D:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:inputtype= "TextPassword" an Droid:password= "true"/> <relativelayOut android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <button Android:id= "@+id/login" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentright= "true" android:layout_alignparenttop= "true" android:layout _marginright= "73DP" android:text= "login"/> <checkbox android:id= "@+id/checkbox1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layou T_alignparentleft= "true" android:checked= "true" android:text= "Remember Password"/> </RelativeLayout> </LinearLayout>
View Effects:
Create a Savefile.java class in the Java file under SRC that is designed to remember how to save the user name, password, and get saved data after the password
Package Com.csdn.www;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.inputstreamreader;import Java.util.hashmap;import Java.util.map;import Android.content.context;public class SaveFile {public static Boolean Save (Context context,string name,string Pass) {try {//file f=new File ("/data/data/com/csdn/www/info.txt"); File F=new file (Context.getfilesdir (), "Info.txt");//context.getfilesdir ();//Returns a directory/data/data/package name/ Filesfileoutputstream fos=new FileOutputStream (f); Fos.write ((name+ "= =" +pass). GetBytes ()); Fos.close (); return true;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return false;}} /** * Get Saved Data * @param context * @return */public static map<string,string> getsavefiles (context context) {File f=new File (Context.getfilesdir (), "Info.txt"), try {FileInputStream fis=new fileinputstream (f); BufferedReader br=new BufferedReader (New InputstreamreadER (FIS)); String Str=br.readline (); String[] infos=str.split ("= ="); Map<string,string> map=new hashmap<string,string> () map.put ("username", infos[0]); Map.put ("Userpass", INFOS[1]); return map;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}
Finally, in the Java file under src Mainactivity.java
Package Com.csdn.www;import Java.util.map;import Android.os.bundle;import android.support.v7.app.ActionBarActivity ; Import Android.text.textutils;import Android.util.log;import android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.checkbox;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Actionbaractivity {private Static final String TAG = "mainactivity";p rivate EditText username, userpass;private CheckBox checkbox1;private Button Log In; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.fragment_main); username = (EditText) This.findviewbyid (r.id.username); userpass = (EditText) This.findviewbyid (r.id.userpass); checkBox1 = (CheckBox) This.findviewbyid (r.id.checkbox1); Map<string, string> map=savefile.getsavefiles (this), if (map!=null) {Username.settext (Map.get ("username")); Userpass.settext (map.geT ("Userpass"));} Login = (Button) Findviewbyid (R.id.login), Login.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {String un = username.gettext (). toString (). Trim (); String up = Userpass.gettext (). toString (). Trim (); if (Textutils.isempty (un) | | Textutils.isempty (UP)) {Toast.maketext (mainactivity.this, "User name or password cannot be empty", Toast.length_short). Show ();} else {//Login// Whether to save the password if (checkbox1.ischecked ()) {//Save user name and password log.i (TAG, "need to save username password"); Boolean Flag=savefile.save (Mainactivity.this, UN, up); if (flag) {Toast.maketext (mainactivity.this, "message saved successfully", 0). Show ();}} Log in to send a message to the server, the server verifies whether the correct if ("Zhangsan". Equals (un) && "123". Equals (UP)) {Toast.maketext (Mainactivity.this, " Login successful ", Toast.length_short). Show ();} else {toast.maketext (mainactivity.this, "Username or password error", Toast.length_short). Show ();}}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
"Message saved successfully", "User name or password error" is displayed
in the "File Explorer" in Eclipse, the directory "Data/data/com/csdn/www/info.txt", user name and password will be saved in Info.txt.