Android Simple Combat Tutorial--The seventh session of "Store user name and password in memory"

Source: Internet
Author: User

The first is the configuration file:

<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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:orientation=" vertical "> <edittext android:id=" @+id/et_name "android:l Ayout_width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter user name"/> <Ed Ittext android:id= "@+id/et_pass" android:layout_width= "match_parent" android:layout_height= "Wrap_cont Ent "android:inputtype=" Textpassword "android:hint=" Please enter the password "/> <relativelayout androi D:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" ><checkbox android:id= "@+id/cb" Android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Remember user name and password" Android:layout_centerverti Cal= "true"/><button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:l Ayout_gravity= "right" android:text= "login" android:layout_alignparentright= "true" android:onclick= "login"/&GT;&L T;/relativelayout></linearlayout>

The code in the activity is as follows:

Package Com.itydl.rwinrom;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.inputstreamreader;import Org.apache.http.entity.inputstreamentity;import Android.os.Bundle;import Android.annotation.suppresslint;import Android.app.activity;import Android.view.menu;import Android.view.View; Import Android.widget.checkbox;import Android.widget.edittext;import Android.widget.toast;public class MainActivity Extends Activity {private EditText et_name;private EditText et_pass; @Override protected void onCreate (Bundle savedi        Nstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);    Et_name = (EditText) Findviewbyid (r.id.et_name);            Et_pass = (EditText) Findviewbyid (R.id.et_pass);            Readaccount ();//The reason for reading in OnCreate is that the activity is created to read the user name and password for Echo. } public void Readaccount () {File File = new File ("Data/data/com.itydl.rwinrom/info.txt"); if (file.exists ()) {try {//FileInputStream FIS = new FileInputStream (file),////convert byte stream to character streams//BufferedReader BR = new Bu        Fferedreader (new//inputstreamreader (FIS)); BufferedReader br = new BufferedReader (new FileReader);//Read user name and password in TXT file string text = Br.readline (); String[] s = Text.split ("# #");//Regular Expression Et_name.settext (S[0]);//ctrl+1 extract global variable Et_pass.settext (s[1]);}    catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}    }} public void login (View v) {String name = Et_name.gettext (). toString ();        String pass = Et_pass.gettext (). toString ();    CheckBox cb = (checkbox) Findviewbyid (R.ID.CB); Determine if the marquee is checked if (cb.ischecked ()) {//data/data/com.itheima.rwinrom: This is the path to the internal storage space File File = new file ("data/data/com. Itydl.rwinrom/info.txt ");//This path is Androut folder FileOutputStream fos;try {fos = new FileOutputStream (file);//check box is checked, The username and password will be stored in the internal storage location Fos.write (name + "# #" + Pass). GetbyTES ()); Fos.close ();}    catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}    }//Create and display the Toast dialog toast.maketext (this, "login succeeded", 0). Show (); }    }

The last is:


When you exit the program and then enter, you will see that the user name and password are echoed.

Android Simple Combat Tutorial--The seventh session of "Store user name and password in memory"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.