Android login implementation, store data to/data/data/package name/info.txt

Source: Internet
Author: User

1. A simple login interface layout code is as follows:

@1 with linear layout and relative layout

@2 linear layout with vertical arrangement

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.market.login.MainActivity">    <EditTextAndroid:id= "@+id/et_name"Android:layout_margintop= "150DP"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    <EditTextAndroid:id= "@+id/et_pwd"Android:layout_margintop= "20DP"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />        <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_margintop= "20DP"Android:layout_height= "Wrap_content">            <CheckBoxAndroid:id= "@+id/cb"Android:layout_width= "Wrap_content"Android:layout_marginleft= "10DP"android:layout_centervertical= "true"Android:layout_height= "Wrap_content"Android:text= "Remember Password"/>            <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Login"Android:layout_marginright= "50DP"Android:layout_centerhorizontal= "true"Android:layout_alignparentright= "true"/>        </Relativelayout></LinearLayout>

The interface effect is as follows:

2. Code logic, divided into Mainactivity class and Saveuserinfo tool class

Three steps: Initializing the UI, initializing the data (loading), initializing the controller

@1 main code is as follows

 PackageCom.market.login;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.util.Map;Import StaticCOM.MARKET.LOGIN.R.ID.CB; Public classMainactivityextendsActivity {PrivateButton btn; PrivateEditText Et_name; PrivateEditText et_pwd; PrivateCheckBox CB; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initview ();    LoadData (); }     Public voidLogin (View v) {String name=Et_name.gettext (). toString (). Trim (); String Password=Et_pwd.gettext (). toString (). Trim (); if(Textutils.isempty (name) | |textutils.isempty (password)) {Toast.maketext ( This, "User name and password cannot be empty", Toast.length_short). Show (); }Else{            if(Cb.ischecked ()) {//If you save your password and name//start saving, save to file, next come in and read this file firstSaveuserinfo.saveinfo (Name,password,true); //submit a user name and password to the serverLOG.V ("Maintivity", "Submit user name and password to server"); }Else{saveuserinfo.saveinfo ("","",false); //submit a user name and password directly to the serverLOG.V ("Maintivity", "Submit user name and password directly to the server"); }        }    }     Public voidInitview () {btn=(Button) Findviewbyid (R.ID.BTN); Et_name=(EditText) Findviewbyid (r.id.et_name); Et_pwd=(EditText) Findviewbyid (R.ID.ET_PWD); CB=(CheckBox) Findviewbyid (R.ID.CB); }     Public voidLoadData () {Map<string, string> info =Saveuserinfo.readinfo (); if(Info! =NULL) {Et_name.settext (Info.get ("Name")); Et_pwd.settext (Info.get ("Password")); Cb.setchecked (Info.get ("isChecked"). Equals ("true"));//If the information is saved, he checked it last time .        }    }}

@2 tool class for saving data to/data/data/package name/Next file

 PackageCom.market.login;ImportAndroid.util.Log;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.HashMap;ImportJava.util.Map;/*** Save user information class, implement save user information to file * Created by Administrator on 2017/6/14.*/ Public classSaveuserinfo {/*Save User Information * Name: username * Password: password * isChecked: Check Save password * **/     Public Static BooleanSaveinfo (String name,string pwd,BooleanisChecked) {String Info= name+ "#" +pwd+ "#" +isChecked; File File=NewFile ("/data/data/com.market.login/info.txt"); FileOutputStream Fos=NULL; Try{fos=Newfileoutputstream (file);            Fos.write (Info.getbytes ()); return true; } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {            if(Fos! =NULL) {                Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); }            }            return false; }    }    /*Read user Information*/     Public StaticMap<string,string>Readinfo () {Map<String,String> map =NULL; File File=NewFile ("/data/data/com.market.login/info.txt"); if(!file.exists ()) {            returnmap; } FileInputStream FIS=NULL; BufferedReader BR=NULL; Try{FIS=Newfileinputstream (file); BR=NewBufferedReader (NewInputStreamReader (FIS)); String Info=Br.readline (); LOG.E ("Saveuserinfo", info); String[] Split= Info.split ("#"); Map=NewHashmap<string,string>(); Map.put ("Name", Split[0]);//Save the Read user name and password into the mapMap.put ("Password", split[1]); Map.put ("IsChecked", split[2]); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {            if(FIS! =NULL) {                Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); }            }            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {e.printstacktrace (); }            }            returnmap; }    }}

3. Operation effect, divided into three kinds of situations

@1 User name and password fill in a blank

@2 does not check the checkbox effect and log back in after exiting the effect

@3 check after effect and exit re-login effect

  

  

Android login implementation, store data to/data/data/package name/info.txt

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.