Android Simple Combat Tutorial--16th words "sharedpreferences Save user name and password"

Source: Internet
Author: User
Tags gettext

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

Where the user name and password are saved in memory, this article saves the user name and password to the sharedpreferences file. In order to be misleading, it is stated that these two methods are not used in real-world development, which indicates a way of thinking and a simple API for beginners to learn.

Because the content is the same as before, do not explain too much. Directly on the code:

XML 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>

Then the mainactivity:

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.inputstreamreader;import Org.apache.http.entity.inputstreamentity;import Com.itheima.sharedpreference.r;import Android.os.Bundle;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;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 ()///Open program echo saved data} public void Readaccount () {Sharedpreferences sp = getsharedpreferences ("info",    Mode_private); String name = sp.getstring ("name", ""),//get data, do not determine whether the file exists.    Because the file does not have a file, you have to bring an empty string "" string pass = sp.getstring ("Pass", "");    Et_name.settext (name); Et_pass.settext (pass);    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 ()) {//Use Sharedpreference to save username and password//path in Data/data/com.itydl.sharedpreference/share_    Sharedpreferences sp = getsharedpreferences ("info", mode_private);    Get the editor editor for sp = Sp.edit ();    Using the editor to access the data, the data is ed.putstring ("name", name), which is accessed as a key-value pair;    Ed.putstring ("Pass", pass);    Submit Ed.commit ();    }//Create and display the Toast dialog toast.maketext (this, "login succeeded", 0). Show (); }    }

Saved path:


Operation Result:


The data will also be successful once you enter the program again




Android Simple Combat Tutorial--16th words "sharedpreferences Save user name and password"

Related Article

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.