当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现带有记住密码功能的登陆界面

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

湖州工长装修,捕鱼游戏 19119澳门公司,快乐大本营5月25日

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

1、设计思路

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

sharedpreferences介绍可以参看这篇博文:

textwatcher的介绍可以参看这篇博文: 

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 start, 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软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网