今日所學:
記住用戶名和登錄密碼
用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>
明日計划:
保存用戶數據到數據庫