利用SharedPreferences登录界面记住密码和自动登录
①修改login_layout.xml文件,增加如下代码,即添加2个勾选框
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/remember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="记住密码"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/auto_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="自动登录"/>
</LinearLayout>
②修改LoginActivity中代码
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将正在创建的活动添加到活动管理器里
ActivityCollector.addActivity(this);
//设置布局
setContentView(R.layout.login_layout);
pref = PreferenceManager.getDefaultSharedPreferences(this);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
autoLogin = (CheckBox)findViewById(R.id.auto_login);
boolean isRemember = pref.getBoolean("remember_password",false);
final boolean isAutologin = pref.getBoolean("auto_login",false);
//得到登录按钮对象
Button login = (Button)findViewById(R.id.login);
Button regist = (Button)findViewById(R.id.regist);
usernameEdit = (EditText)findViewById(R.id.username);
passwordEdit = (EditText)findViewById(R.id.password);
if (isRemember){
//将账号和密码都设置到文本框中
String Username = pref.getString("username","");
String Password = pref.getString("password","");
usernameEdit.setText(Username);
passwordEdit.setText(Password);
rememberPass.setChecked(true);
if (isAutologin){
//自动登录跳转
autoLogin.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginActivity.this,IndexActivity.class);
LoginActivity.this.startActivity(intent);
}
}
//给登录按钮设置监听器
login.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//得到用户名和密码的编辑框
usernameEdit = (EditText)findViewById(R.id.username);
passwordEdit = (EditText)findViewById(R.id.password);
//判断是否成功匹配的标志
boolean flag = false;
//LitePal里遍历查询所有数据的方法
List<User> users = DataSupport.findAll(User.class);
for (User user : users){
//判断用户输入的用户名和密码是否与数据库中相同
if(user.getUsername().equals(usernameEdit.getText().toString())&&
user.getPassword().equals(passwordEdit.getText().toString())) {
flag = true;
}}
if(flag){
editor = pref.edit();
if (rememberPass.isChecked()){//检查复选框是否被选中
editor.putBoolean("remember_password",true);
editor.putString("username",usernameEdit.getText().toString());
editor.putString("password",passwordEdit.getText().toString());
}else {
editor.putBoolean("remember_password",false);
//editor.clear();
//用clear会删除所有Shared文件数据,不适合
}
editor.apply();
if (autoLogin.isChecked()){
editor.putBoolean("auto_login",true);
}else {
editor.putBoolean("auto_login",false);
}
editor.apply();
ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setTitle("正在登陆");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
//创建Intent对象,传入源Activity和目的Activity的类对象
Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
String USERNAME = usernameEdit.getText().toString();
intent.putExtra("userName",USERNAME);
//启动Activity
startActivity(intent);
}else{
//登录信息错误,通过Toast显示提示信息
Toast.makeText(LoginActivity.this,"用户登录信息错误" , Toast.LENGTH_SHORT).show();
}
}
});
regist.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
startActivity(intent);
}
});
}
...
效果如图
选择记住密码后,第一次登录成功,再次开启应用,密码显示
选择自动登录后,第一次登录成功,再次开启应用直接跳转到主界面