用 SharedPreferences 儲存進行記住密碼 和儲存使用者名稱(記住密碼)
阿新 • • 發佈:2019-01-05
1.什麼是SharedPreferences 儲存?
SharedPreferences是Android平臺上一個輕量級的儲存類,用來儲存少量資料時簡單,便捷(儲存記住密碼狀態,設定開關狀態等)。
以key-value(鍵值對)形式儲存資料,可以儲存的資料型別為:String、float、int、long、boolean。
儲存位置在/data/data/<包名>/shared_prefs目錄下。
儲存的資料以XML形式儲存。
2.使用SharedPreferences寫入資料步驟:
1.獲得使用SharedPreferences物件;
2.獲得Editor物件;
3.通過Editor物件的putXXX函式,設定寫入資料;
4.通過Editor物件的commit提交寫入;
獲取SharePreferences物件
在有Context環境中使用getSharePreferences方法獲得
獲取Editor物件
通過SharePreferences.edit()方法獲得
使用SharedPreferences儲存資料案例
package com.example.myapplication15;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText edit_username;
private EditText edit_password;
private CheckBox checkBox;
private Button button;
private CheckBox checkBox_zidong;
private int remamberFlag = 0;
private String password = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binID();
SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
//如果不為空
if (sharedPreferences != null) {
String name = sharedPreferences.getString("name", "");
password = sharedPreferences.getString("password", "");
remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
edit_username.setText(name);
}
//確定為1獲取 記住密碼,打鉤
if (remamberFlag == 1) {
checkBox.setChecked(true);
edit_password.setText(password);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1 建立 SharePreferences 物件
String username = edit_username.getText().toString();
String password = edit_password.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//2 建立Editor物件,寫入值
editor.putString("name", username);
if (checkBox.isChecked()) {
remamberFlag = 1;
editor.putInt("remeber_flag", remamberFlag);
editor.putString("password", password);
} else {
remamberFlag = 0;
editor.putInt("remeber_flag", remamberFlag);
}
//3 提交
editor.commit();
Toast.makeText(MainActivity.this, "登入成功", Toast.LENGTH_LONG).show();
}
});
}
private void binID() {
edit_username = findViewById(R.id.edit_username);
edit_password = findViewById(R.id.edit_password);
checkBox = findViewById(R.id.Cb);
checkBox_zidong = findViewById(R.id.zidong_check);
button = findViewById(R.id.main_button);
}
}
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="com.example.myapplication15.MainActivity">
<RelativeLayout
android:layout_marginTop="200dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_username"
android:hint="輸入賬號"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="50dp" />
</RelativeLayout>
<RelativeLayout
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_password"
android:hint="輸入密碼"
android:password="true"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="50dp" />
</RelativeLayout>
<Button
android:id="@+id/main_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="登入"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<CheckBox
android:id="@+id/Cb"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_marginLeft="60dp"
/>
<TextView
android:textColor="#000000"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="記住密碼"
/>
<CheckBox
android:id="@+id/zidong_check"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_marginLeft="60dp"
/>
<TextView
android:textColor="#000000"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="自動登入"
/>
</LinearLayout>
</LinearLayout>