下面是用SharedPreFerences存储用户名和密码,可以自动登录和记住密码
1、布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/pwd_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/remeberPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@+id/remeberAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<Button
android:id="@+id/log_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录" />
2、MainActivity
package com.wjh.activity;
public class MainActivity extends Activity {
private CheckBox remeberPwd;
private Editor editor;
private EditText name;
private EditText pwd;
private CheckBox remeberAuto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
name = (EditText) findViewById(R.id.name_main);
pwd = (EditText) findViewById(R.id.pwd_main);
remeberPwd = (CheckBox) findViewById(R.id.remeberPwd);
remeberAuto = (CheckBox) findViewById(R.id.remeberAuto);
Button log = (Button) findViewById(R.id.log_main);
// 获取pref对象
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
//获取操作类
editor = pref.edit();
// 记住密码
boolean isRemeberPwd = pref.getBoolean("isRemeberPwd", false);
if (isRemeberPwd) {
//获取用户名和密码
String userName = pref.getString("userName", "");
String userPwd = pref.getString("userPwd", "");
//想输入框设置内容
name.setText(userName);
pwd.setText(userPwd);
//下次自动在记住密码上打钩
remeberPwd.setChecked(true);
}
// 自动登录
boolean isRemeberAuto = pref.getBoolean("isRemeberAuto", false);
if (isRemeberAuto) {
//因练习所有模拟数据
Toast.makeText(MainActivity.this, "跳转成功", 0).show();
}
// 如果自动登录选中记住密码
remeberAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
remeberPwd.setChecked(true);
} else {
remeberPwd.setChecked(false);
}
}
});
// 点击登录
log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (remeberPwd.isChecked()) {
//获取输入框的用户名和密码
String name2 = name.getText().toString();
String pwd2 = pwd.getText().toString();
//添加进库
editor.putString("userName", name2);
editor.putString("userPwd", pwd2);
editor.putBoolean("isRemeberPwd", true);
//提交,editor.clear可以清楚记住密码
editor.commit();
}
if (remeberAuto.isChecked()) {
editor.putBoolean("isRemeberAuto", true);
editor.commit();
}
// 跳转
Toast.makeText(MainActivity.this, "登录成", 0).show();
}
});
}
}