今日所学:
记住用户名和登录密码
用adb查看保存文件内容
遇到的问题:
用adb查看文件时,没有权限访问data文件
出现原因:google play虚拟机没有root权限
解决办法:
用google API的虚拟机
相关博客:adb shell中提示Permission denied - 简书
关于/system/bin/sh: su: not found的解决办法(安卓模拟器运行) - 程序员大本营
成果截图:

代码:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText mEtPhone;
private EditText mEtPasswd;
private CheckBox mPsd;
private String SP_PHONE="sp_phone";
private String SP_PASSWD="sp_passwd";
private SharedPreferences sharedPreferences;
private String SP_IS_REMEMBER="sp_is_remember";
private boolean isCheck=false;
private String TAG="MainActivity";
@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);
}
mEtPhone.setText(sharedPreferences.getString(SP_PHONE,""));
mEtPasswd.setText(sharedPreferences.getString(SP_PASSWD,""));
isCheck=sharedPreferences.getBoolean(SP_IS_REMEMBER,false);
Log.i(TAG,"测试:"+isCheck);
mPsd.setChecked(isCheck);
}
private void initUI() {
mEtPhone=findViewById(R.id.et_phone);
mEtPhone.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(isCheck){
if(sharedPreferences==null){
sharedPreferences=getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit=sharedPreferences.edit();
edit. putString(SP_PHONE,mEtPhone.getText().toString());
edit.commit();
}
}
});
mEtPasswd=findViewById(R.id.et_passwd);
mEtPasswd.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(isCheck){
if(sharedPreferences==null){
sharedPreferences=getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit=sharedPreferences.edit();
edit.putString(SP_PASSWD,mEtPasswd.getText().toString());
edit.commit();
}
}
});
mPsd=findViewById(R.id.cb_remember_psd);
mPsd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG,"状态为:"+isChecked);
isCheck=isChecked;
if(sharedPreferences==null){
sharedPreferences=getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor edit=sharedPreferences.edit();
if(isChecked){
edit. putString(SP_PHONE,mEtPhone.getText().toString());
edit.putString(SP_PASSWD,mEtPasswd.getText().toString());
}
edit.putBoolean(SP_IS_REMEMBER,isChecked);
edit.commit();
}
});
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_phone"
android:inputType="phone"
android:hint="电话"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/et_passwd"
android:inputType="textPassword"
android:hint="密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/cb_remember_psd"
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
