使用SharedPreferences實現簡單的記住用戶名和密碼功能
SharedPreferences是Android系統中提供的一種使用鍵值對形式存儲數據的方式,特點是使用簡單方便,缺點是由於功能簡單,存儲的數據形式有限,只能用作簡單的數據存儲。
使用方法:
- 直接聲明一個SharedPreferences對象
- 使用getSharedPreferences();方法獲取實例
- 使用SharedPreferences中的edit方法進行數據的編輯、添加、刪除和存儲
下面我們使用SharedPreferences方式來實現一個簡單的記住用戶名和密碼的功能來學習其用法:
Talk is cheap, Let`s Code!
首先我們編寫一個登陸界面,XML代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="100dp"
android:text="用戶名"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:hint="在這裏輸入你的用戶名" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="密碼"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:hint="在這裏輸入你的密碼"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:checked="false"
android:text="記住用戶名" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:checked="false"
android:text="記住密碼" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="登陸" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="取消" />
</LinearLayout>
</LinearLayout>
代碼看起來有點長,實際上很簡單,就是在一個Linearlayout中嵌套了四個子Linearlayout,實現了一個登陸界面佈局。如圖:
然後我們在Activity中實現登陸邏輯。並在代碼中講解使用方法:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button login, cancel;
private CheckBox checkBox, checkBox2;
private EditText editName, editPassword;
//聲明一個SharedPreferences對象和一個Editor對象
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
cancel = (Button) findViewById(R.id.cancel);
editName = (EditText) findViewById(R.id.editName);
editPassword = (EditText) findViewById(R.id.editPassword);
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
//獲取preferences和editor對象
preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
editor = preferences.edit();
/*
啓動程序時首先檢查sharedPreferences中是否儲存有用戶名和密碼
若無,則將checkbox狀態顯示爲未選中
若有,則直接中sharedPreferences中讀取用戶名和密碼,並將checkbox狀態顯示爲已選中
這裏getString()方法需要兩個參數,第一個是鍵,第二個是值。
啓動程序時我們傳入需要讀取的鍵,值填null即可。若有值則會自動顯示,沒有則爲空。
*/
String name = preferences.getString("userName",null);
if (name == null) {
checkBox.setChecked(false);
} else {
editName.setText(name);
checkBox.setChecked(true);
}
String password = preferences.getString("userPassword", null);
if (password == null) {
checkBox2.setChecked(false);
} else {
editPassword.setText(password);
checkBox2.setChecked(true);
}
//爲login和cancel設置監聽事件
login.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//判斷用戶是進行的是登陸操作還是取消操作
switch (v.getId()) {
case R.id.login:
String name = editName.getText().toString().trim();
String password = editPassword.getText().toString().trim();
//驗證用戶名和密碼,若爲admin-123456即可登錄
if (name.equals("admin") && password.equals("123456")) {
if (checkBox.isChecked()) {
//如果用戶選擇了記住用戶名
//將用戶輸入的用戶名存入儲存中,鍵爲userName
editor.putString("userName", name);
editor.commit();
} else {
//否則將用戶名清除
editor.remove("userName");
editor.commit();
}
if (checkBox2.isChecked()) {
//如果用戶選擇了記住密碼
//將用戶輸入的密碼存入儲存中,鍵爲userName
editor.putString("userPassword", password);
editor.commit();
} else {
//否則將密碼清除
editor.remove("userPassword");
editor.commit();
}
//提示登陸成功
Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show();
} else {
//若登陸不成功,則將錯誤的用戶名和密碼清除,並提示登陸失敗
editor.remove("userName");
editor.remove("userPassword");
editor.commit();
Toast.makeText(this, "login failed", Toast.LENGTH_SHORT).show();
}
break;
//若用戶選擇了取消,則直接退出登錄
case R.id.cancel:
finish();
}
}
}
代碼邏輯也十分簡單,一個是登陸並決定是否保存用戶名和密碼,一個是程序啓動時自動檢查儲存中是否存有用戶名和密碼,並顯示相應的checkbox狀態。
使用ShardPreferences要注意的幾點:
- SharedPreferences對象本身只能讀取,而不能編輯、刪除和儲存,要操作數據必須使用edit方法。
- 使用edit方法操作數據之後一定要使用Preferences中的edito.commit();方法提交,才能成功操作。
- SharedPreferences使用鍵值對的方式儲存數據,因此無法操作過於複雜的數據。
下面來看一下Demo運行效果:
由於簡書上傳圖片大小限制,所以只能分幾段進行演示
還有密碼或用戶名輸入錯誤會自動清除所輸入的用戶名和密碼的Demo就不演示了,有興趣的朋友可以自行實驗。
這樣一個簡單的使用SharedPreferences實現的記住用戶名和密碼的登陸功能就實現了。
完。