Android Case 2: Simple login screen and save information

Source: Internet
Author: User
Tags gettext

Interface effect:

Layout code:

<LinearLayoutxmlns: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:orientation= "vertical"Tools:context=". Mainactivity " >    <EditTextAndroid:id= "@+id/et_username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name" />    <EditTextAndroid:id= "@+id/et_userpassword"Android:password= "true"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password" />    <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_margintop= "20DP" >        <CheckBoxAndroid:id= "@+id/cb_ischeck"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Remember user name password" />        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:onclick= "Login"Android:text= "Login" />    </Relativelayout></LinearLayout>

Mainactivity:

 PackageCom.dreamtech.login;ImportJava.util.Map;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_name; PrivateEditText Et_password; PrivateCheckBox Cb_ischeck; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Et_name=(EditText) Findviewbyid (r.id.et_username); Et_password=(EditText) Findviewbyid (R.id.et_userpassword); Cb_ischeck=(CheckBox) Findviewbyid (R.id.cb_ischeck); //read the saved dataMap<string, string> maps = Userinfoutils.readinfo (mainactivity. This); if(Maps! =NULL) {            //removedString name = Maps.get ("name"); String pwd= Maps.get ("pwd");            Et_name.settext (name);        Et_password.settext (PWD); }    }    //Click events     Public voidLogin (View v) {String name=Et_name.gettext (). toString (). Trim (); String pwd=Et_password.gettext (). toString (). Trim (); //determine if the user name password is empty        if(Textutils.isempty (name) | |Textutils.isempty (PWD)) {Toast.maketext (mainactivity). This, "The user name or password cannot be empty", Toast.length_long). Show (); } Else {            //This is not a database operation, just a simple login logicSYSTEM.OUT.PRINTLN ("Connection Server Authentication"); //stored on-premises            if(cb_ischeck.ischecked ()) {Booleanresult = Userinfoutils.saveinfo (mainactivity. This, name, PWD); if(Result) {Toast.maketext (mainactivity. This, "Saved successfully! ", Toast.length_long). Show (); } Else{toast.maketext (mainactivity). This, "Save Failed", Toast.length_long). Show (); }            } Else{toast.maketext (mainactivity). This, "Please tick", Toast.length_long). Show (); }        }    }}

Read-Write File tool class:

 PackageCom.dreamtech.login;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;ImportJava.util.HashMap;ImportJava.util.Map;ImportAndroid.content.Context; Public classUserinfoutils {//Save Data Tool class     Public Static BooleanSaveinfo (context context, string Username, string pwd) {Try{String path=Context.getfilesdir (). GetPath (); String result= Username + "&&" +pwd; File File=NewFile (Path, "Info.txt"); FileOutputStream Fos=Newfileoutputstream (file);            Fos.write (Result.getbytes ());            Fos.close (); return true; } Catch(Exception e) {e.printstacktrace (); return false; }    }     Public StaticMap<string, string>Readinfo (Context context) {Try{String path=Context.getfilesdir (). GetPath (); Map<string, string> maps =NewHashmap<string, string>(); File File=NewFile (Path, "Info.txt"); FileInputStream FIS=Newfileinputstream (file); BufferedReader BUFR=NewBufferedReader (NewInputStreamReader (FIS)); String content=Bufr.readline (); String[] Splits= Content.split ("&&"); String name= Splits[0]; String pwd= Splits[1]; Maps.put ("Name", name); Maps.put ("PWD", PWD);            Fis.close (); returnmaps; } Catch(Exception e) {e.printstacktrace (); return NULL; }    }}

Android Case 2: Simple login screen and save information

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.