Android实现带有记住密码功能的登陆界面

Android实现带有记住密码功能的登陆界面,第1张

概述本文实例为大家分享了Android带有记住密码功能的登陆界面实现代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID带有记住密码功能的登陆界面实现代码,供大家参考,具体内容如下

1、设计思路

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

SharedPreferences介绍可以参看这篇博文:http://www.jb51.net/article/84859.htm

TextWatcher的介绍可以参看这篇博文:http://www.jb51.net/article/84865.htm 

2、功能介绍

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

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

3、效果图

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

/*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; import androID.Widget.button; import androID.Widget.CheckBox; import androID.Widget.EditText; import androID.Widget.Toast;  public class RemeberPwdActivity extends Activity {   autoCompleteTextVIEw cardNumauto;  EditText passwordET;  button logBT;   CheckBox savePasswordCB;  SharedPreferences sp;  String cardNumStr;  String passwordStr;   /** Called when the activity is first created. */  @OverrIDe  public 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);// 默认为记住密码  cardNumauto.setThreshold(1);// 输入1个字母就开始自动提示  passwordET.setinputType(inputType.TYPE_CLASS_TEXT  | inputType.TYPE_TEXT_VARIATION_PASSWORD);  // 隐藏密码为inputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81  // 显示密码为inputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91   cardNumauto.addTextChangedListener(new TextWatcher() {   @OverrIDe  public voID onTextChanged(CharSequence s,int start,int before,int count) {  // Todo auto-generated method stub  String[] allUsername = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少个键值对  allUsername = sp.getAll().keySet().toArray(new String[0]);  // sp.getAll()返回一张hash map  // keySet()得到的是a set of the keys.  // hash map是由key-value组成的   ArrayAdapter<String> adapter = new ArrayAdapter<String>(   RemeberPwdActivity.this,androID.R.layout.simple_dropdown_item_1line,allUsername);   cardNumauto.setAdapter(adapter);// 设置数据适配器   }   @OverrIDe  public voID beforeTextChanged(CharSequence s,int count,int after) {  // Todo auto-generated method stub   }   @OverrIDe  public voID afterTextChanged(Editable s) {  // Todo auto-generated method stub  passwordET.setText(sp.getString(cardNumauto.getText()   .toString(),""));// 自动输入密码   }  });   // 登陆  logBT.setonClickListener(new OnClickListener() {   @OverrIDe  public voID onClick(VIEw v) {  // Todo auto-generated method stub   cardNumStr = cardNumauto.getText().toString();  passwordStr = passwordET.getText().toString();   if (!((cardNumStr.equals("test")) && (passwordStr   .equals("test")))) {   Toast.makeText(RemeberPwdActivity.this,"密码错误,请重新输入",Toast.LENGTH_SHORT).show();  } else {   if (savePasswordCB.isChecked()) {// 登陆成功才保存密码   sp.edit().putString(cardNumStr,passwordStr).commit();   }   Toast.makeText(RemeberPwdActivity.this,"登陆成功,正在获取用户数据……",Toast.LENGTH_SHORT).show();   // 跳转到另一个Activity   // do something   }   }  });   }  } 

布局文件: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="简单登陆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="用 户 名:"   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="用户密码:"   androID:textSize="15px" />    <EditText   androID:ID="@+ID/passwordET"   androID:layout_wIDth="fill_parent"   androID:layout_height="40px" >   </EditText>   </linearLayout>  </linearLayout>  </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="记住密码" >  </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="登录" >  </button>  </linearLayout>  </linearLayout>  </linearLayout>  </linearLayout> 

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

<?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> 

以上就是本文的全部内容,希望对大家学习AndroID软件编程有所帮助。

总结

以上是内存溢出为你收集整理的Android实现带有记住密码功能的登陆界面全部内容,希望文章能够帮你解决Android实现带有记住密码功能的登陆界面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1149221.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存