安卓对本地文件的存储和读取,实现密码自动填充
package com.example.filelianxi;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SharedPreferencesTest extends AppCompatActivity {
private EditText et_pwd;
private String pwd;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private CheckBox remember_pass;
private Button login;
private String account;
private String password;
@Override
protected void onCreate(Bundle saveadInstanceState){
super.onCreate(saveadInstanceState);
setContentView(R.layout.activity_shared_preferences_test);
Button saveData = findViewById(R.id.save_data);
//et_pwd =findViewById(R.id.et_pwd);
pref = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = findViewById(R.id.et_name);
passwordEdit = findViewById(R.id.et_pwd);
remember_pass = findViewById(R.id.remember_pass);
login = findViewById(R.id.login);
boolean isRemember = pref.getBoolean("Remmember_pwd",false);
if (isRemember) {
String account =pref.getString("account","");
String password = pref.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
remember_pass.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
account =accountEdit.getText().toString();
password =passwordEdit.getText().toString();
if (account.equals("admin")&&password.equals("123456")){
editor = pref.edit();
if (remember_pass.isChecked()){
editor.putBoolean("Remmember_pwd",true);
editor.putString("account",account);
editor.putString("password",password);
}else{
editor.clear();
}
editor.apply();
Toast.makeText(SharedPreferencesTest.this,"登录成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SharedPreferencesTest.this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
});
saveData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name","tom");
editor.putInt("age",28);
editor.putString("pwd","123456789");
editor.putBoolean("married",false);
editor.apply();
editor.commit();
}
});
Button restoreData = findViewById(R.id.restore_data);
restoreData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String name = pref.getString("name",null);
int age = pref.getInt("age",0);
boolean married = pref.getBoolean("married",false);
pwd = pref.getString("pwd",null);
Log.d("Maintivity",name+age+married+pwd);
Log.d("shu",pwd+"");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/et_name"
android:text=""
/>
<EditText
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:password="true"
android:id="@+id/et_pwd"
android:text=""
/>
<CheckBox
android:id="@+id/remember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存密码"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="登录"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:id="@+id/save_data"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/restore_data"
/>
</LinearLayout>