Android Learning Minitwitter Remember the password

Source: Internet
Author: User

Today Xiao-Minitwitter learned the small case of password remembering:

The interface is as follows:

There are 2 points to be aware of at design time:

1. Do remember the password only after the user has chosen to remember the password and log on successfully

2. Remember not only the user name and password, but also the check box selection status

The layout main code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@drawable/btnbg_roundcorner"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin" > <TextView Android:id= "@+id/tvusername"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:text= "@string/tvname"android:textappearance= "Android:attr/textappearancemedium"/> <EditText Android:id= "@+id/etusername"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/tvusername"Android:layout_below= "@+id/tvusername"Android:background= "@android:d rawable/edit_text"Android:ems= "Ten" > <requestfocus/> </EditText> <TextView Android:id= "@+id/tvpassword"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/etusername"Android:layout_below= "@+id/etusername"Android:text= "@string/tvpassword"android:textappearance= "Android:attr/textappearancemedium"/> <EditText Android:id= "@+id/etpassword"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/tvpassword"Android:layout_below= "@+id/tvpassword"Android:layout_margintop= "16DP"Android:background= "@android:d rawable/edit_text"Android:ems= "10"Android:inputtype= "Textpassword"/> <Button Android:id= "@+id/btnlogin"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignright= "@+id/etpassword"Android:layout_below= "@+id/etpassword"Android:layout_margintop= "20DP"Android:background= "#FF72CAE1"Android:text= "@string/btnlogin"/> <CheckBox Android:id= "@+id/rememberpassword"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/etpassword"Android:layout_aligntop= "@+id/btnlogin"Android:layout_marginleft= "19DP"android:checked= "true"Android:text= "@string/remember"/></relativelayout>
<pre name= "Code"class= "html" ><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:orientation= "Vertical"Android:background= "@drawable/LOGINBG"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=". Loginactivity "> <include layout=" @layout/login_top "/> <include layout=" @layout/login_bottom "/>"</LinearLayout>

The main code to implement the login and save password is as follows:

ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;Importandroid.text.Editable;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classLoginactivityextendsActivity {PrivateButton Btnlogin;PrivateEditText Etusername;PrivateEditText Etpassword;PrivateCheckBox Rememberpassword;//declares a sharedpreferences for storing dataPrivateSharedpreferences setting =NULL;Private Static FinalString prefs_name = "Namepwd"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main); Etusername=(EditText) Findviewbyid (r.id.etusername); Etpassword=(EditText) Findviewbyid (R.id.etpassword); Rememberpassword=(CheckBox) Findviewbyid (R.id.rememberpassword); Btnlogin=(Button) Findviewbyid (R.id.btnlogin);        Setlistener ();    GetData (); }    Private voidSetlistener () {//binding Events for login buttonsBtnlogin.setonclicklistener (NewOnclicklistener () {String username=Etusername.gettext (). toString (); String Password=Etpassword.gettext (). toString (); @Override Public voidOnClick (View arg0) {if(! (Textutils.isempty (username)) &&(textutils.isempty (password))) {                //determine user name and password                if("123". Equals (username) && "123". Equals (password)) {                                       //Jump to homepageIntent Intent =NewIntent (loginactivity. This, login2activity.class);                    StartActivity (Intent);                Finish (); }                 }                Else                 {                    //Show Error PromptToast.maketext (Getapplicationcontext (), "User name or password error", Toast.length_short). Show ();    }                            }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    protected voidOnresume () {//get the data stored before the interface displays the data        Super. Onresume ();    GetData (); }    /*** Get Storage is data*/    Private voidGetData () {//Get Sharedpreferences Objectsetting =getsharedpreferences (Prefs_name, mode_private); //determine if a user name password was previously stored        if(Setting.getboolean ("Iskeep",false)) {            //if it was previously stored, it is displayed in the corresponding text boxEtusername.settext (setting.getstring ("username", "" ")); Etpassword.settext (Setting.getstring ("Password", "" ")); } Else {            //Otherwise, NULL is displayedEtusername.settext (""); Etpassword.settext (""); }    }}

Android Learning Minitwitter Remember the password

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.