实现一个记住密码的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、效果图:




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


[java]  view plain copy
  1. /*author: conowen 
  2.  * date: 2012.4.2 
  3.  *  
  4.  */  
  5. package com.conowen.remeberPwd;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.SharedPreferences;  
  9. import android.os.Bundle;  
  10. import android.text.Editable;  
  11. import android.text.InputType;  
  12. import android.text.TextWatcher;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.ArrayAdapter;  
  16. import android.widget.AutoCompleteTextView;  
  17. import android.widget.Button;  
  18. import android.widget.CheckBox;  
  19. import android.widget.EditText;  
  20. import android.widget.Toast;  
  21.   
  22. public class RemeberPwdActivity extends Activity {  
  23.   
  24.     AutoCompleteTextView cardNumAuto;  
  25.     EditText passwordET;  
  26.     Button logBT;  
  27.   
  28.     CheckBox savePasswordCB;  
  29.     SharedPreferences sp;  
  30.     String cardNumStr;  
  31.     String passwordStr;  
  32.   
  33.     /** Called when the activity is first created. */  
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.         cardNumAuto = (AutoCompleteTextView) findViewById(R.id.cardNumAuto);  
  39.         passwordET = (EditText) findViewById(R.id.passwordET);  
  40.         logBT = (Button) findViewById(R.id.logBT);  
  41.   
  42.         sp = this.getSharedPreferences("passwordFile", MODE_PRIVATE);  
  43.         savePasswordCB = (CheckBox) findViewById(R.id.savePasswordCB);  
  44.         savePasswordCB.setChecked(true);// 默认为记住密码  
  45.         cardNumAuto.setThreshold(1);// 输入1个字母就开始自动提示  
  46.         passwordET.setInputType(InputType.TYPE_CLASS_TEXT  
  47.                 | InputType.TYPE_TEXT_VARIATION_PASSWORD);  
  48.         // 隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81  
  49.         // 显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91  
  50.   
  51.         cardNumAuto.addTextChangedListener(new TextWatcher() {  
  52.   
  53.             @Override  
  54.             public void onTextChanged(CharSequence s, int start, int before,  
  55.                     int count) {  
  56.                 // TODO Auto-generated method stub  
  57.                 String[] allUserName = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少个键值对  
  58.                 allUserName = sp.getAll().keySet().toArray(new String[0]);  
  59.                 // sp.getAll()返回一张hash map  
  60.                 // keySet()得到的是a set of the keys.  
  61.                 // hash map是由key-value组成的  
  62.   
  63.                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
  64.                         RemeberPwdActivity.this,  
  65.                         android.R.layout.simple_dropdown_item_1line,  
  66.                         allUserName);  
  67.   
  68.                 cardNumAuto.setAdapter(adapter);// 设置数据适配器  
  69.   
  70.             }  
  71.   
  72.             @Override  
  73.             public void beforeTextChanged(CharSequence s, int start, int count,  
  74.                     int after) {  
  75.                 // TODO Auto-generated method stub  
  76.   
  77.             }  
  78.   
  79.             @Override  
  80.             public void afterTextChanged(Editable s) {  
  81.                 // TODO Auto-generated method stub  
  82.                 passwordET.setText(sp.getString(cardNumAuto.getText()  
  83.                         .toString(), ""));// 自动输入密码  
  84.   
  85.             }  
  86.         });  
  87.   
  88.         // 登陆  
  89.         logBT.setOnClickListener(new OnClickListener() {  
  90.   
  91.             @Override  
  92.             public void onClick(View v) {  
  93.                 // TODO Auto-generated method stub  
  94.   
  95.                 cardNumStr = cardNumAuto.getText().toString();  
  96.                 passwordStr = passwordET.getText().toString();  
  97.   
  98.                 if (!((cardNumStr.equals("test")) && (passwordStr  
  99.                         .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

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center_horizontal"  
  11.         android:text="简单登陆DEMO"  
  12.         android:textSize="25px" />  
  13.   
  14.     <LinearLayout  
  15.         xmlns:android="http://schemas.android.com/apk/res/android"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"  
  18.         android:gravity="center"  
  19.         android:orientation="vertical" >  
  20.   
  21.         <LinearLayout  
  22.             android:layout_width="250dip"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_marginLeft="10dp"  
  25.             android:layout_marginRight="10dp"  
  26.             android:layout_marginTop="15dp"  
  27.             android:orientation="vertical" >  
  28.   
  29.             <LinearLayout  
  30.                 android:layout_width="fill_parent"  
  31.                 android:layout_height="wrap_content"  
  32.                 android:orientation="horizontal" >  
  33.   
  34.                 <LinearLayout  
  35.                     android:layout_width="fill_parent"  
  36.                     android:layout_height="80px"  
  37.                     android:orientation="vertical" >  
  38.   
  39.                     <LinearLayout  
  40.                         android:layout_width="fill_parent"  
  41.                         android:layout_height="40px"  
  42.                         android:orientation="horizontal" >  
  43.   
  44.                         <TextView  
  45.                             android:id="@+id/tv_account"  
  46.                             android:layout_width="wrap_content"  
  47.                             android:layout_height="wrap_content"  
  48.                             android:layout_marginRight="10dp"  
  49.                             android:text="用  户  名:"  
  50.                             android:textSize="15px" />  
  51.   
  52.                         <AutoCompleteTextView  
  53.                             android:id="@+id/cardNumAuto"  
  54.                             android:layout_width="fill_parent"  
  55.                             android:layout_height="40px" >  
  56.                         </AutoCompleteTextView>  
  57.                     </LinearLayout>  
  58.   
  59.                     <LinearLayout  
  60.                         android:layout_width="fill_parent"  
  61.                         android:layout_height="40px"  
  62.                         android:orientation="horizontal" >  
  63.   
  64.                         <TextView  
  65.                             android:layout_width="wrap_content"  
  66.                             android:layout_height="wrap_content"  
  67.                             android:layout_marginRight="10dp"  
  68.                             android:text="用户密码:"  
  69.                             android:textSize="15px" />  
  70.   
  71.                         <EditText  
  72.                             android:id="@+id/passwordET"  
  73.                             android:layout_width="fill_parent"  
  74.                             android:layout_height="40px" >  
  75.                         </EditText>  
  76.                     </LinearLayout>  
  77.                 </LinearLayout>  
  78.             </LinearLayout>  
  79.   
  80.             <LinearLayout  
  81.                 android:layout_width="wrap_content"  
  82.                 android:layout_height="wrap_content"  
  83.                 android:orientation="horizontal" >  
  84.   
  85.                 <CheckBox  
  86.                     android:id="@+id/savePasswordCB"  
  87.                     android:layout_width="wrap_content"  
  88.                     android:layout_height="wrap_content"  
  89.                     android:layout_marginLeft="20dp"  
  90.                     android:text="记住密码" >  
  91.                 </CheckBox>  
  92.   
  93.                 <Button  
  94.                     android:id="@+id/logBT"  
  95.                     android:layout_width="100px"  
  96.                     android:layout_height="wrap_content"  
  97.                     android:layout_marginLeft="40dp"  
  98.                     android:layout_marginRight="10dp"  
  99.                     android:text="登录" >  
  100.                 </Button>  
  101.             </LinearLayout>  
  102.         </LinearLayout>  
  103.     </LinearLayout>  
  104.   
  105. </LinearLayout>  


SharedPreferences文件,在/data/data/包名/shared_prefs文件夹下面

[java]  view plain copy
  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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值