我們?cè)谥暗膶W(xué)習(xí)中寫(xiě)過(guò)一個(gè)強(qiáng)制下線(xiàn)的demo虏等,其中涉及到了一個(gè)模擬的登陸功能食绿。那么今天梳凛,在原有的基礎(chǔ)上,我們通過(guò)SharedPreferences來(lái)實(shí)現(xiàn)一個(gè)能夠記住登錄名和密碼的功能疙教。
為了區(qū)分之前的登陸頁(yè)面棺聊,我們新建一個(gè)LoginAdvActivity表示更高級(jí)的登陸,修改布局文件:
<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"
tools:context="com.johnhao.listviewdemo.activity.LoginActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用戶(hù)名"
android:textSize="20sp"/>
<EditText
android:id="@+id/edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="用戶(hù)名試試 tester"
android:textSize="18sp"
android:maxLength="20"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密 碼"
android:textSize="20sp"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="登陸密碼試試 123456"
android:textSize="18sp"
android:maxLength="20"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="remeber password"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#ffff00"
android:text="Login"
android:textAllCaps="false"
android:textColor="#000000"/>
<ImageView
android:src="@drawable/qrcode"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
</LinearLayout>
相對(duì)于原來(lái)的布局贞谓,我們?cè)黾恿艘粋€(gè)checkbox控件和一行文案限佩。checkbox是一個(gè)復(fù)選框,用戶(hù)可以通過(guò)點(diǎn)擊的方式進(jìn)行選中或取下裸弦,我們就用這個(gè)控件表示用戶(hù)是否勾選了記住密碼功能祟同。接下來(lái)修改LoginAdvActivity代碼:
public class LoginAdvActivity extends AppCompatActivity {
private Button btn;
private EditText editUserName;
private EditText editserPassword;
private CheckBox rememberPass;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_adv);
setTitle("登陸");
btn = findViewById(R.id.btn_login);
editUserName = findViewById(R.id.edit_name);
editserPassword = findViewById(R.id.edit_password);
rememberPass = findViewById(R.id.checkbox);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = prefs.getBoolean("remeber_password", false);
if (isRemember) {
String account = prefs.getString("account", "");
String password = prefs.getString("password", "");
editUserName.setText(account);
editserPassword.setText(password);
rememberPass.setChecked(true);
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = editUserName.getText().toString();
String userPassword = editserPassword.getText().toString();
if (userName.equals("tester") && userPassword.equals("123456")) {
editor = prefs.edit();
if (rememberPass.isChecked()) {
editor.putBoolean("remeber_password", true);
editor.putString("account", userName);
editor.putString("password", userPassword);
} else {
editor.clear();
}
editor.apply();
// 登陸成功
Intent intent = new Intent(LoginAdvActivity.this, AfterLoginActivity.class);
startActivity(intent);
finish();
} else if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPassword)) {
// 用戶(hù)名或密碼為空
Toast.makeText(LoginAdvActivity.this, "請(qǐng)輸入用戶(hù)名或者密碼", Toast.LENGTH_SHORT).show();
} else {
// 用戶(hù)名或密碼不符合
Toast.makeText(LoginAdvActivity.this, "請(qǐng)輸入正確的用戶(hù)名和密碼", Toast.LENGTH_SHORT).show();
editUserName.setText("");
editserPassword.setText("");
}
}
});
}
}
首先在onCreate()方法中通過(guò)getDefaultSharedPreferences()得到了SharedPreferences對(duì)象,然后通過(guò)getBoolean獲取remeber_password鍵對(duì)應(yīng)的值理疙。因?yàn)槭堑谝淮蔚顷懺纬牵隙](méi)有任何數(shù)據(jù),因此設(shè)置默認(rèn)的值的false窖贤。在點(diǎn)擊事件中砖顷,登陸成功后,我們先通過(guò)調(diào)用SharedPreferences的edit()得到一個(gè)SharedPreferences.Editor對(duì)象主之,然后判斷checkbox是否是被選中狀態(tài)择吊。如果被選中,說(shuō)明用戶(hù)想保存密碼槽奕,這樣我就調(diào)用一系列put方法將數(shù)據(jù)存到SharedPreferences并提交几睛。如果checkbox沒(méi)有被選中,我們則調(diào)用clear()方法將數(shù)據(jù)清空粤攒。
當(dāng)用戶(hù)保存了密碼并成功登錄后所森,remeber_password的值就為true了,這樣讓用戶(hù)再次到登陸頁(yè)面的時(shí)候夯接,就會(huì)自動(dòng)的從SharedPreferences讀取數(shù)據(jù)焕济,并將對(duì)應(yīng)的數(shù)據(jù)填充到輸入框中,由此實(shí)現(xiàn)簡(jiǎn)單的記住登陸密碼的功能盔几。
重新運(yùn)行下程序晴弃,記住登陸密碼:
取消記住登陸密碼:
簡(jiǎn)單的實(shí)例就這樣,但是在實(shí)際的項(xiàng)目中不能這么應(yīng)用逊拍。涉及到敏感的信息都需要進(jìn)行加密處理來(lái)保護(hù)數(shù)據(jù)的安全性上鞠。