Android---5---saving and echoing data

Source: Internet
Author: User

Do a simple QQ landing interface, the ability to save the account number and password to the phone, and start the program again will automatically display the last saved account and password.




Activitymain.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.qqlogin.MainActivity "> <edittext android:layout_width=" fill_p Arent "android:layout_height=" wrap_content "android:id=" @+id/number "android:hint=" QQ number/Mobile phone number/mailbox "/&G     T;<edittext android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/psw"        Android:inputtype= "Textpassword" android:hint= "password"/><checkbox android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text= "Remember Password" android:checked= "true" android:id= "@+id/remember"/><button android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:id=" @+id/login "android:text=" Login "/></LinearLayou T>

Mainactivity.java


Package Com.example.qqlogin;import Java.util.map;import Com.example.qqlogin.utils.utils;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.util.Log;import Android.view.menu;import Android.view.menuitem;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;/** * * @author Caesar * */public class Mainactivity extends Activity implements Onclicklistener {PR Ivate EditText numbertext;private EditText pswtext;private Button button;private CheckBox cb;protected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); numberText = (EditText) Findviewbyid (r.id.number);p Swtext = (EditText) Findviewbyid (R.ID.PSW); cb = (CheckBox) Findviewbyid ( R.id.remember); button = (button) Findviewbyid (R.id.login); Button.setonclicklistener (this);//Echo Data map<string, string> uSerinfomap = Utils. GetUserInfo (this), if (userinfomap! = null) {Numbertext.settext (Userinfomap.get ("number"));p Swtext.settext ( Userinfomap.get ("PSW"));}} public void OnClick (View v) {String number = Numbertext.gettext (). toString (); String PSW = Pswtext.gettext (). toString (); String Info = "Account" + number + "password" + PSW; Toast.maketext (this, Info, 0). Show (); if (textutils.isempty (number) | | Textutils.isempty (PSW)) {Toast.maketext (this, "Please fill in correctly", 0). Show (); if (cb.ischecked ()) {if (utils). Saveuserinfo (This,number, PSW)) Toast.maketext (this, "Save succeeded", 0). Show (); else {Toast.maketext (this, "Save Failed", 0). Show ();}} Toast.maketext (This, "Login succeeded", 1). Show ();}}


Utils.java

Package Com.example.qqlogin.utils;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.io.outputstream;import java.util.hashmap;import java.util.map;import Android.*;import android.content.Context Import android.text.textutils;import android.widget.toast;/** * Phone memory file read * @author Caesar * */public class Utils {/** * Save file to Phone memory * @param context * @param number * @param PSW * @return */public Static Boolean Saveuserinfo (context context, St Ring number,string PSW) {try {File filedir = Context.getfilesdir (); File F = new file (Filedir, "data.txt"); FileOutputStream Fos;fos = new FileOutputStream (f); String data = number + "# #" + Psw;fos.write (Data.getbytes ()); Fos.flush (); Fos.close (); return true;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return false;} /** * Read from phone memory * @param context * @return */public static map<string,String> GetUserInfo (Context context) {try {File filedir = Context.getfilesdir (); File F = new file (Filedir, "data.txt"); FileInputStream Fis;fis = new FileInputStream (f); BufferedReader reader = new BufferedReader (new InputStreamReader (FIS)); string text = Reader.readline (), if (!text.isempty ()) {String split[] = Text.split ("# #"); map<string, string> userinfomap = new hashmap<string, string> (); Userinfomap.put ("number", split[0]); Userinfomap.put ("PSW", split[1]); return userinfomap;}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}


Android---5---saving and echoing data

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.