remember_pwd(activity)
package com.example.myapplication_one;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class remember_pwd extends AppCompatActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private SharedPreferences pref;//通过pref读取SharedPreferences的数据
private SharedPreferences.Editor editor;//editor将数据写入SharedPreferences
private CheckBox rememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember_pwd);
pref= PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPass=(CheckBox)findViewById(R.id.remember_pass);
login = (Button) findViewById(R.id.login);
boolean isRemenber=pref.getBoolean("remember_password",false);//读取上次登陆时存入"remember_password"的信息,没有读取到则默认为false
if(isRemenber)//如果读取为true,则将account和password,checkbox的信息写入文本框
{
String account=pref.getString("account","");
String password=pref.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String accout = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
if (accout.equals("1") && password.equals("1")) {
editor=pref.edit();
if(rememberPass.isChecked()){//如果勾选了checkbox框,则将account,password,checkbox信息写入
editor.putBoolean("remember_password",true);
editor.putString("account",accout);
editor.putString("password",password);
}else {
editor.clear();//若没有,清除SharedPreferences存储的信息
}
editor.apply();
Intent intent = new Intent(remember_pwd.this, MainActivity6.class);
startActivity(intent);
finish();
} else
Toast.makeText(remember_pwd.this, "密码输入错误", Toast.LENGTH_SHORT).show();
}
});
}
}
remember_pwd.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:text="用户名"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="center"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:id="@+id/account"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:text="密码"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="center"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:id="@+id/password"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/remember_pass"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="记住密码"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登录"
android:id="@+id/login"/>
</LinearLayout>
运行结果: