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

所属分类: 软件编程 / Android 阅读数: 1574
收藏 0 赞 0 分享

本文实例为大家分享了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软件编程有所帮助。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多