Android Hands-on tutorial seventh how to store user names and passwords in memory _android

Source: Internet
Author: User

The example of this article for everyone to share the Android memory stored username and password method for your reference, the specific content as follows

First, 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:lay" 
    Out_width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter username"/> Android:id= "@+id/et_pass" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Andr Oid:inputtype= "Textpassword" android:hint= "Please enter the password"/> <relativelayout android:layout_width= "Match_ 
  Parent "android:layout_height=" Wrap_content "  android:orientation= "Horizontal" > <checkbox android:id= "@+id/cb" W Rap_content "android:layout_height=" wrap_content "android:text= remember username and password" android:layout_centervertic Al= "true"/> <button android:layout_width= wrap_content "android:layout_height=" Wrap_cont Ent "android:layout_gravity=" right "android:text=" login "android:layout_alignparentright=" true "a 
 ndroid:onclick= "Login"/> </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 savedinstancestate) {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 ()//is read in OnCreate because the activity is created to read the username 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 flows to character streams//BufferedReader BR = new BufferedReader (New//InputStreamReader (FIS)); 
        BufferedReader br = new BufferedReader (new FileReader (file)); 
        Read the username 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 block E.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 = new file ("Data/data/com.itydl.rwinrom/info.txt");//This path is a Androut folder Fileoutputstre 
      Am FOS; 
        try {fos = new FileOutputStream (file); 
        check box is checked, the user name password will be stored in the internal storage location Fos.write (name + "# #" + Pass). GetBytes ()); 
      Fos.close (); 
      catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); 
  }//Create and display the Toast dialog box Toast.maketext (this, login succeeded, 0). Show (); 
 } 
   
}

Finally, the screenshot:


When you exit the program and then enter, you will see that both the username and password are echoing.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.