Android Study Notes (19) ---- implement an android login interface that remembers the password

Source: Internet
Author: User
Tags md5 encryption

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.

**************************************** **************************************** ************/

1. design ideas:

Sharedpreferences is used to store user data. This demo is not encrypted. Once the Android system is root, other users can view the user's private directory, and the password file is insecure. Therefore, if the real application is on the software, it must be encrypted before it is saved. You can choose MD5 encryption.

Sharedpreferences introduction can see this blog: http://blog.csdn.net/conowen/article/details/7312612

For more information about textwatcher, see this blog: http://blog.csdn.net/conowen/article/details/7420673

2. Features

By default, the "Remember password" check box is selected, and the "login" button is clicked. Once logon is successful, the user name and password are saved to the sharedpreferences file.

When the user name is entered, the user data is continuously read through textwatcher and the corresponding "User Name" is automatically prompted. After the user name is selected, the sharedpreferences file is read and the password is automatically entered.

3 ,:


4. Code: the details are in the comments.

/* Author: conowen * Date: 2012.4.2 **/package COM. conowen. remeberpwd; import android. app. activity; import android. content. sharedpreferences; import android. OS. bundle; import android. text. editable; import android. text. inputtype; import android. text. textwatcher; import android. view. view; import android. view. view. onclicklistener; import android. widget. arrayadapter; import android. widget. autocompletetextview; Im Port android. widget. button; import android. widget. checkbox; import android. widget. edittext; import android. widget. toast; public class extends activity {extends cardnumauto; edittext passwordet; button logbt; checkbox savepasswordcb; sharedpreferences sp; string cardnumstr; string passwordstr;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle Savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); cardnumauto = (autocompletetextview) findviewbyid (R. id. cardnumauto); passwordet = (edittext) findviewbyid (R. id. passwordet); logbt = (button) findviewbyid (R. id. logbt); SP = This. getsharedpreferences ("passwordfile", mode_private); savepasswordcb = (checkbox) findviewbyid (R. id. savepasswordcb); savepasswordcb. setchecked (True); // the default password is cardnumauto. setthreshold (1); // enter one letter to start automatically prompting passwordet. setinputtype (inputtype. type_class_text | inputtype. type_text_variation_password); // hide the password as inputtype. type_text_variation_password, that is, 0x81 // The password is inputtype. type_text_variation_visible_password, that is, 0x91cardnumauto. addtextchangedlistener (New textwatcher () {@ overridepublic void ontextchanged (charsequence S, int start, int before, int Co Unt) {// todo auto-generated method stubstring [] allusername = new string [Sp. getall (). size ()]; // sp. getall (). size () returns the number of key-value pairs allusername = sp. getall (). keyset (). toarray (New String [0]); // sp. getall () returns a hash map // keyset () to obtain a set of the keys. // hash map is an arrayadapter consisting of key-value <string> adapter = new arrayadapter <string> (remeberpwdactivity. this, android. r. layout. simple_dropdown_item_1line, allusern AME); cardnumauto. setadapter (adapter); // set the data adapter} @ overridepublic void beforetextchanged (charsequence S, int start, int count, int after) {// todo auto-generated method stub} @ overridepublic void aftertextchanged (editable s) {// todo auto-generated method stubpasswordet. settext (sp. getstring (cardnumauto. gettext (). tostring (), ""); // automatically enter the password}); // log on to logbt. setonclicklistener (New onclicklistener () {@ overri Depublic void onclick (view v) {// todo auto-generated method stubcardnumstr = cardnumauto. gettext (). tostring (); passwordstr = passwordet. gettext (). tostring (); If (! (Cardnumstr. equals ("test") & (passwordstr. equals ("test") {toast. maketext (remeberpwdactivity. this, "Incorrect password, please enter again", toast. length_short ). show ();} else {If (savepasswordcb. ischecked () {// The password sp. edit (). putstring (cardnumstr, passwordstr ). commit ();} toast. maketext (remeberpwdactivity. this, "Login successful, getting user data ...... ", Toast. length_short). Show (); // jump to another activity // do something }}});}}

Layout file: Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal" Android: text = "simple Login Demo" Android: textsize = "25px"/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center" Android: orientation = "vertical"> <linearlayout Android: layout_width = "250dip" Android: layout_height = "wrap_content" Android: layout_marginleft = "10dp" Android: layout_marginright = "10dp" Android: layout_margintop = "15dp" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "80px" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "40px" Android: Orientation = "horizontal"> <textview Android: Id = "@ + ID/TV _account" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginright = "10dp" Android: text = "User name:" Android: textsize = "15px"/> <autocompletetextview Android: Id = "@ + ID/cardnumauto" Android: layout_width = "fill_parent" Android: layout_height = "40px"> </autocompletetextview> </linearlayout> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "40px" Android: orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginright = "10dp" Android: text = "User Password:" Android: textsize = "15px"/> <edittext Android: Id = "@ + ID/passwordet" Android: layout_width = "fill_parent" Android: layout_height = "40px"> </edittext> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <checkbox Android: Id = "@ + ID/savepasswordcb" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "20dp" Android: text = "Remember password"> </checkbox> <button Android: Id = "@ + ID/logbt" Android: layout_width = "100px" Android: layout_height = "wrap_content" Android: layout_marginleft = "40dp" Android: layout_marginright = "10dp" Android: TEXT = "login"> </button> </linearlayout>

The sharedpreferences file is in the/data/package name/shared_prefs folder.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="test">test</string><string name="test2">test</string><string name="test1">test</string></map>

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.