Android实现一个记住密码的登陆界面

1、设计思路:

主要采用SharedPreferences来保存用户数据,本Demo没有经过加密,所有一旦Android系统被ROOT的话,其他用户就可以查看用户的私有目录,密码文件就很不安全。所以真正应用在软件上面的,一定要经过加密才保存,可以选择MD5加密。

 

SharedPreferences介绍可以参看这篇博文:http://blog.csdn.net/conowen/article/details/7312612

TextWatcher的介绍可以参看这篇博文:http://blog.csdn.net/conowen/article/details/7420673

 

2、功能介绍

默认勾选“记住密码”复选框,点击“登陆”按钮,一旦成功登陆,就保存用户名和密码到SharedPreferences文件中。

 

用户名输入时,通过TextWatcher不断去读取用户数据,自动提示相应的“用户名”,选择了用户名之后,就会读取SharedPreferences的文件,然后自动完成密码的输入。

 

3、效果图:

Android实现一个记住密码的登陆界面_第1张图片

 

Android实现一个记住密码的登陆界面_第2张图片

 

4、代码:详细都在注释里面了

001 /*author: conowen
002  * date: 2012.4.2
003  
004  */
005 package com.conowen.remeberPwd;
006   
007 import android.app.Activity;
008 import android.content.SharedPreferences;
009 import android.os.Bundle;
010 import android.text.Editable;
011 import android.text.InputType;
012 import android.text.TextWatcher;
013 import android.view.View;
014 import android.view.View.OnClickListener;
015 import android.widget.ArrayAdapter;
016 import android.widget.AutoCompleteTextView;
017 import android.widget.Button;
018 import android.widget.CheckBox;
019 import android.widget.EditText;
020 import android.widget.Toast;
021   
022 public class RemeberPwdActivity extends Activity {
023   
024     AutoCompleteTextView cardNumAuto;
025     EditText passwordET;
026     Button logBT;
027   
028     CheckBox savePasswordCB;
029     SharedPreferences sp;
030     String cardNumStr;
031     String passwordStr;
032   
033     /** Called when the activity is first created. */
034     @Override
035     public void onCreate(Bundle savedInstanceState) {
036         super.onCreate(savedInstanceState);
037         setContentView(R.layout.main);
038         cardNumAuto = (AutoCompleteTextView) findViewById(R.id.cardNumAuto);
039         passwordET = (EditText) findViewById(R.id.passwordET);
040         logBT = (Button) findViewById(R.id.logBT);
041   
042         sp = this.getSharedPreferences("passwordFile", MODE_PRIVATE);
043         savePasswordCB = (CheckBox) findViewById(R.id.savePasswordCB);
044         savePasswordCB.setChecked(true);// 默认为记住密码
045         cardNumAuto.setThreshold(1);// 输入1个字母就开始自动提示
046         passwordET.setInputType(InputType.TYPE_CLASS_TEXT
047                 | InputType.TYPE_TEXT_VARIATION_PASSWORD);
048         // 隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81
049         // 显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91
050   
051         cardNumAuto.addTextChangedListener(new TextWatcher() {
052   
053             @Override
054             public void onTextChanged(CharSequence s, int start, int before,
055                     int count) {
056                 // TODO Auto-generated method stub
057                 String[] allUserName = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少个键值对
058                 allUserName = sp.getAll().keySet().toArray(new String[0]);
059                 // sp.getAll()返回一张hash map
060                 // keySet()得到的是a set of the keys.
061                 // hash map是由key-value组成的
062   
063                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
064                         RemeberPwdActivity.this,
065                         android.R.layout.simple_dropdown_item_1line,
066                         allUserName);
067   
068                 cardNumAuto.setAdapter(adapter);// 设置数据适配器
069   
070             }
071   
072             @Override
073             public void beforeTextChanged(CharSequence s, int start, int count,
074                     int after) {
075                 // TODO Auto-generated method stub
076   
077             }
078   
079             @Override
080             public void afterTextChanged(Editable s) {
081                 // TODO Auto-generated method stub
082                 passwordET.setText(sp.getString(cardNumAuto.getText()
083                         .toString(), ""));// 自动输入密码
084   
085             }
086         });
087   
088         // 登陆
089         logBT.setOnClickListener(new OnClickListener() {
090   
091             @Override
092             public void onClick(View v) {
093                 // TODO Auto-generated method stub
094   
095                 cardNumStr = cardNumAuto.getText().toString();
096                 passwordStr = passwordET.getText().toString();
097   
098                 if (!((cardNumStr.equals("test")) && (passwordStr
099                         .equals("test")))) {
100                     Toast.makeText(RemeberPwdActivity.this, "密码错误,请重新输入",
101                             Toast.LENGTH_SHORT).show();
102                 } else {
103                     if (savePasswordCB.isChecked()) {// 登陆成功才保存密码
104                         sp.edit().putString(cardNumStr, passwordStr).commit();
105                     }
106                     Toast.makeText(RemeberPwdActivity.this, "登陆成功,正在获取用户数据……",
107                             Toast.LENGTH_SHORT).show();
108                     // 跳转到另一个Activity
109                     // do something
110   
111                 }
112   
113             }
114         });
115   
116     }
117   
118 }
布局文件:main.xml
查看源码
打印 ?
001 <?xml version="1.0" encoding="utf-8"?>
002 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
003     android:layout_width="fill_parent"
004     android:layout_height="fill_parent"
005     android:orientation="vertical" >
006   
007     <TextView
008         android:layout_width="wrap_content"
009         android:layout_height="wrap_content"
010         android:layout_gravity="center_horizontal"
011         android:text="简单登陆DEMO"
012         android:textSize="25px" />
013   
014     <LinearLayout
015         xmlns:android="http://schemas.android.com/apk/res/android"
016         android:layout_width="fill_parent"
017         android:layout_height="fill_parent"
018         android:gravity="center"
019         android:orientation="vertical" >
020   
021         <LinearLayout
022             android:layout_width="250dip"
023             android:layout_height="wrap_content"
024             android:layout_marginLeft="10dp"
025             android:layout_marginRight="10dp"
026             android:layout_marginTop="15dp"
027             android:orientation="vertical" >
028   
029             <LinearLayout
030                 android:layout_width="fill_parent"
031                 android:layout_height="wrap_content"
032                 android:orientation="horizontal" >
033   
034                 <LinearLayout
035                     android:layout_width="fill_parent"
036                     android:layout_height="80px"
037                     android:orientation="vertical" >
038   
039                     <LinearLayout
040                         android:layout_width="fill_parent"
041                         android:layout_height="40px"
042                         android:orientation="horizontal" >
043   
044                         <TextView
045                             android:id="@+id/tv_account"
046                             android:layout_width="wrap_content"
047                             android:layout_height="wrap_content"
048                             android:layout_marginRight="10dp"
049                             android:text="用  户  名:"
050                             android:textSize="15px" />
051   
052                         <AutoCompleteTextView
053                             android:id="@+id/cardNumAuto"
054                             android:layout_width="fill_parent"
055                             android:layout_height="40px" >
056                         </AutoCompleteTextView>
057                     </LinearLayout>
058   
059                     <LinearLayout
060                         android:layout_width="fill_parent"
061                         android:layout_height="40px"
062                         android:orientation="horizontal" >
063   
064                         <TextView
065                             android:layout_width="wrap_content"
066                             android:layout_height="wrap_content"
067                             android:layout_marginRight="10dp"
068                             android:text="用户密码:"
069                             android:textSize="15px" />
070   
071                         <EditText
072                             android:id="@+id/passwordET"
073                             android:layout_width="fill_parent"
074                             android:layout_height="40px" >
075                         </EditText>
076                     </LinearLayout>
077                 </LinearLayout>
078             </LinearLayout>
079   
080             <LinearLayout
081                 android:layout_width="wrap_content"
082                 android:layout_height="wrap_content"
083                 android:orientation="horizontal" >
084   
085                 <CheckBox
086                     android:id="@+id/savePasswordCB"
087                     android:layout_width="wrap_content"
088                     android:layout_height="wrap_content"
089                     android:layout_marginLeft="20dp"
090                     android:text="记住密码" >
091                 </CheckBox>
092   
093                 <Button
094                     android:id="@+id/logBT"
095                     android:layout_width="100px"
096                     android:layout_height="wrap_content"
097                     android:layout_marginLeft="40dp"
098                     android:layout_marginRight="10dp"
099                     android:text="登录" >
100                 </Button>
101             </LinearLayout>
102         </LinearLayout>
103     </LinearLayout>
104   
105 </LinearLayout>
SharedPreferences文件,在/data/data/包名/shared_prefs文件夹下面
 
1 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
2 <map>
3 <string name="test">test</string>
4 <string name="test2">test</string>
5 <string name="test1">test</string>
6 </map>

你可能感兴趣的:(android应用)