安卓保存与回显账号密码
使用SharedPreferences存放变量
public class MainActivity extends AppCompatActivity {
private EditText phone;
private EditText psw;
private CheckBox check;
private SharedPreferences sharedPreferences;
private String TAG = "MainActivity";
private String SP_PHONE= "sp_phone";
private String SP_PSW= "sp_psw";
private String SP_IS_REMEMBER ="SP_IS_REMEMBER" ;
private boolean IsChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
}
private void initData() {
if(sharedPreferences==null){
sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
phone.setText(sharedPreferences.getString(SP_PHONE,""));
psw.setText(sharedPreferences.getString(SP_PSW,""));
IsChecked = sharedPreferences.getBoolean(SP_IS_REMEMBER,false);
check.setChecked(IsChecked);
}
private void initUI() {
phone = findViewById(R.id.phone);
phone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(IsChecked){
if(sharedPreferences==null){
sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(SP_PHONE,phone.getText().toString());
edit.commit();
}
}
});
psw = findViewById(R.id.psw);
psw.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(IsChecked){
if(sharedPreferences==null){
sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(SP_PSW,psw.getText().toString());
edit.commit();
}
}
});
check = findViewById(R.id.rem);
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG,"状态为:"+isChecked);
IsChecked = isChecked;
if(isChecked){
sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
if(sharedPreferences==null){
sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(SP_PHONE,phone.getText().toString());
edit.putString(SP_PSW,psw.getText().toString());
edit.putBoolean(SP_IS_REMEMBER, isChecked);
edit.commit();
}
}
});
}
}
页面代码:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/phone"
android:inputType="text"
android:hint="电话"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/psw"
android:inputType="textPassword"
android:hint="密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/rem"
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
/>
</LinearLayout>