`
lovnet
  • 浏览: 6709729 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

实现一个记住密码的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、代码:详细都在注释里面了


  1. /*author:conowen
  2. *date:2012.4.2
  3. *
  4. */
  5. packagecom.conowen.remeberPwd;
  6. importandroid.app.Activity;
  7. importandroid.content.SharedPreferences;
  8. importandroid.os.Bundle;
  9. importandroid.text.Editable;
  10. importandroid.text.InputType;
  11. importandroid.text.TextWatcher;
  12. importandroid.view.View;
  13. importandroid.view.View.OnClickListener;
  14. importandroid.widget.ArrayAdapter;
  15. importandroid.widget.AutoCompleteTextView;
  16. importandroid.widget.Button;
  17. importandroid.widget.CheckBox;
  18. importandroid.widget.EditText;
  19. importandroid.widget.Toast;
  20. publicclassRemeberPwdActivityextendsActivity{
  21. AutoCompleteTextViewcardNumAuto;
  22. EditTextpasswordET;
  23. ButtonlogBT;
  24. CheckBoxsavePasswordCB;
  25. SharedPreferencessp;
  26. StringcardNumStr;
  27. StringpasswordStr;
  28. /**Calledwhentheactivityisfirstcreated.*/
  29. @Override
  30. publicvoidonCreate(BundlesavedInstanceState){
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. cardNumAuto=(AutoCompleteTextView)findViewById(R.id.cardNumAuto);
  34. passwordET=(EditText)findViewById(R.id.passwordET);
  35. logBT=(Button)findViewById(R.id.logBT);
  36. sp=this.getSharedPreferences("passwordFile",MODE_PRIVATE);
  37. savePasswordCB=(CheckBox)findViewById(R.id.savePasswordCB);
  38. savePasswordCB.setChecked(true);//默认为记住密码
  39. cardNumAuto.setThreshold(1);//输入1个字母就开始自动提示
  40. passwordET.setInputType(InputType.TYPE_CLASS_TEXT
  41. |InputType.TYPE_TEXT_VARIATION_PASSWORD);
  42. //隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81
  43. //显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91
  44. cardNumAuto.addTextChangedListener(newTextWatcher(){
  45. @Override
  46. publicvoidonTextChanged(CharSequences,intstart,intbefore,
  47. intcount){
  48. //TODOAuto-generatedmethodstub
  49. String[]allUserName=newString[sp.getAll().size()];//sp.getAll().size()返回的是有多少个键值对
  50. allUserName=sp.getAll().keySet().toArray(newString[0]);
  51. //sp.getAll()返回一张hashmap
  52. //keySet()得到的是asetofthekeys.
  53. //hashmap是由key-value组成的
  54. ArrayAdapter<String>adapter=newArrayAdapter<String>(
  55. RemeberPwdActivity.this,
  56. android.R.layout.simple_dropdown_item_1line,
  57. allUserName);
  58. cardNumAuto.setAdapter(adapter);//设置数据适配器
  59. }
  60. @Override
  61. publicvoidbeforeTextChanged(CharSequences,intstart,intcount,
  62. intafter){
  63. //TODOAuto-generatedmethodstub
  64. }
  65. @Override
  66. publicvoidafterTextChanged(Editables){
  67. //TODOAuto-generatedmethodstub
  68. passwordET.setText(sp.getString(cardNumAuto.getText()
  69. .toString(),""));//自动输入密码
  70. }
  71. });
  72. //登陆
  73. logBT.setOnClickListener(newOnClickListener(){
  74. @Override
  75. publicvoidonClick(Viewv){
  76. //TODOAuto-generatedmethodstub
  77. cardNumStr=cardNumAuto.getText().toString();
  78. passwordStr=passwordET.getText().toString();
  79. if(!((cardNumStr.equals("test"))&&(passwordStr
  80. .equals("test")))){
  81. Toast.makeText(RemeberPwdActivity.this,"密码错误,请重新输入",
  82. Toast.LENGTH_SHORT).show();
  83. }else{
  84. if(savePasswordCB.isChecked()){//登陆成功才保存密码
  85. sp.edit().putString(cardNumStr,passwordStr).commit();
  86. }
  87. Toast.makeText(RemeberPwdActivity.this,"登陆成功,正在获取用户数据……",
  88. Toast.LENGTH_SHORT).show();
  89. //跳转到另一个Activity
  90. //dosomething
  91. }
  92. }
  93. });
  94. }
  95. }

布局文件:main.xml

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


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

  1. <?xmlversion='1.0'encoding='utf-8'standalone='yes'?>
  2. <map>
  3. <stringname="test">test</string>
  4. <stringname="test2">test</string>
  5. <stringname="test1">test</string>
  6. </map>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics