一、序言
Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中则存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。
二、SharedPreference介绍
首先我们要知道,Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。
2.1 getSharedPreferences 两个参数的含义
public SharedPreferences getSharedPreferences(String name, int mode) {
return sp.getSharedPreferences(name, mode);
}
(1)name为本组件的配置文件名( 自己定义,也就是一个文件名)
(2)mode为操作模式,默认的模式为0或MODE_PRIVATE,还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE
2.1.1 mode参数值的含义
mode指定为MODE_PRIVATE,则该配置文件只能被自己的应用程序访问;
mode指定为MODE_WORLD_READABLE,则该配置文件除了自己访问外还可以被其它应该程序读取;
mode指定为MODE_WORLD_WRITEABLE,则该配置文件除了自己访问外还可以被其它应该程序读取和写入;
三、代码实现
3.1 代码分析
(1)判断是否输入了账号和密码;
if(usr.trim().equals("")){
Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show();
return;
}
if(pwd.trim().equals("")){
Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show();
return;
}
(2)在layout_main.xml定义一个 CheckBox;
<CheckBox
android:id="@+id/login_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
(3)实现CheckBox的事件响应处理;
//CheckBox的点击响应函数
public void lookCheckBox(String usrStr, String pswStr){
boolean checkBoxLogin = checkBox.isChecked();
if(checkBoxLogin){//CheckBox被选中,读取sp的内容,下次进入时会显示账号和密码
SharedPreferences.Editor editor = sp.edit();
editor.putString("usr", usrStr);
editor.putString("psw", pswStr);
editor.putBoolean("checkBoxBoolean", true);
editor.commit();
} else {//CheckBox未被选中,清空sp的内容,下次进入时不会显示账号和密码
SharedPreferences.Editor editor = sp.edit();
editor.putString("usr", null);
editor.putString("psw", null);
editor.putBoolean("checkBoxBoolean", false);
editor.commit();
}
}
(4)SharedPreference 的存储实现;
//先定义
SharedPreferences sp = null;
//访问一个名为userinfo.xml的文件,用于存储sp的信息;若没有,则新建
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
//对usr 和 pwd 的操作
if (sp.getBoolean("checkboxBoolean", false)){
//若获取的checkboxBoolean值为true,则显示账号、密码
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
3.2 完整代码
(1)activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:id="@+id/login_usr"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="请输入用户名"
android:inputType="text"
android:maxLength="11"
android:maxLines="1"
android:paddingLeft="18dp"
android:textColor="@color/blueSky"
android:textSize="16sp" />
<EditText
android:id="@+id/login_pwd"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:paddingLeft="16dp"
android:textColor="@color/blueSky"
android:textSize="16sp" />
<CheckBox
android:id="@+id/login_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<!-- 登录按钮 -->
<Button
android:id="@+id/login_login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/login_btn_bg"
android:text="登录"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
(2)LoginActivity.java
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
initView();
}
//获取控件、添加监听器
private void initView() {
usrEt = findViewById(R.id.login_usr);
pwdEt = findViewById(R.id.login_pwd);
checkBox = findViewById(R.id.login_checkbox);
loginBtn = findViewById(R.id.login_login_btn);
loginBtn.setOnClickListener(this);
if(sp.getBoolean("checkBoxBoolean",false)){
usrEt.setText(sp.getString("usr", null));
pwdEt.setText(sp.getString("psw", null));
checkBox.setChecked(true);
}
}
//点击响应
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_login_btn:
loginCheck();
break;
}
}
/*登录验证*/
private void loginCheck() {
//获取用户填入的登录信息
String usrStr = usrEt.getText().toString();
boolean checkusr = TextUtils.isEmpty(usrStr);
String pswStr = pwdEt.getText().toString();
boolean checkpsw = TextUtils.isEmpty(pswStr);
if(checkusr) {
ToastUtil.showLong("账号不能为空!");
}else if (checkpsw) {
ToastUtil.showLong("密码不能为空!");
}
//验证成功,跳转
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
lookCheckBox(usrStr, pswStr);
}
//记住密码CheckBox响应函数
public void lookCheckBox(String usrStr, String pswStr){
boolean checkBoxLogin = checkBox.isChecked();
if(checkBoxLogin){
SharedPreferences.Editor editor = sp.edit();
editor.putString("usr", usrStr);
editor.putString("psw", pswStr);
editor.putBoolean("checkBoxBoolean", true);
editor.commit();
}else{
SharedPreferences.Editor editor = sp.edit();
editor.putString("usr", null);
editor.putString("psw", null);
editor.putBoolean("checkBoxBoolean", false);
editor.commit();
}
}
(3)MainActivity.java
import android.app.Activity;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持!