文章目錄
- 1 `SharedPreferences` 介紹
- 1.1 `SharedPreferences` 四種操作模式
- 1.3 使用方法
- 2 使用 `SharedPreferences` 進行登錄
- 2.1 前端設計
- 2.1 Control層
1 SharedPreferences 介紹
- SharedPreferences是使用鍵值對的方式來存盤資料的
SharedPreferences share = getSharedPreferences("my_file", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = share.edit();
// 4 保存資料到檔案
editor.putString("account", input_account.getText().toString());
editor.putString("password", input_password.getText().toString());
editor.putBoolean("pass_remem", pass_remem.isChecked()); // 單選框 選中時回傳為 true
- 當保存一條資料的時候,需要給這條資料提供一個對應的鍵,可以通過這個鍵把相應的值取出來
SharedPreferences sharedPreferences = getSharedPreferences("my_file", Context.MODE_PRIVATE);
Boolean pass_remem_ = sharedPreferences.getBoolean("pass_remem", false);
- 它是一個輕量級的存盤類,特別適合用于保存軟體配置引數,使用SharedPreferences保存資料,檔案存放在
/data/data/<package name>/shared_prefs目錄下
1.1 SharedPreferences 四種操作模式
-
Context.MODE_PRIVATE:為默認操作模式,代表該檔案是私有資料,只能被應用本身訪問,在該模式下,寫入的內容會覆寫原檔案的內容
-
Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就創建新檔案.
-
MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取.
-
MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入.
1.3 使用方法
- 由于SharedPreferences是一個介面,而且在這個介面里沒有提供寫入資料和讀取資料的能力,但其內部有一個Editor內部介面,Editor介面有一系列方法來操作SharedPreference
-
edit( ) 獲得SharedPreferences.Edit物件
getSharedPreferences("myfile",0).edit( ) -
向物件中添加資料
- putString( )
- putInt( )
- putBoolean( )
editor.putString(“name”, “張三");
editor.putInt(“age”, 21);
editor.putBoolean("married",true)
-
commit( ) 提交資料,完成資料存盤操作
editor.commit( ); -
從檔案中讀取資料 第一個引數為KEY 第二個引數為訪問失敗時的默認值
- getString( )
- getInt( )
- getBoolean( )
getString ("name", "");
getInt (“age", 0);
getBoolean (“married", false);
2 使用 SharedPreferences 進行登錄
2.1 前端設計

<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="36sp"
android:gravity="center"
android:layout_marginTop="100dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/S_account"
android:gravity="center"
android:textSize="16sp"
/>
<EditText
android:id="@+id/input_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:textSize="16sp"
android:paddingLeft="10dp"
android:inputType="textPersonName"
android:hint="@string/S_input_account"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/S_password"
android:gravity="center"
android:textSize="16sp"
/>
<EditText
android:id="@+id/input_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:textSize="16sp"
android:paddingLeft="10dp"
android:inputType="numberPassword"
android:hint="@string/S_input_password"/>
</LinearLayout>
<CheckBox
android:id="@+id/password_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_gravity="right"
android:layout_marginRight="30dp"
android:layout_marginBottom="33dp"
android:text="@string/S_pass_remem"
android:checked="false"
/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="20dp"
android:text="@string/S_button_submit"
android:textSize="24sp"
/>
</LinearLayout>
2.1 Control層
public class MainActivity extends AppCompatActivity {
private EditText input_account, input_password;
private CheckBox pass_remem;
private Button submit_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1 獲取各個組件的資訊, 并存盤到資料層
input_account = this.findViewById(R.id.input_account);
input_password = this.findViewById(R.id.input_password);
pass_remem = this.findViewById(R.id.password_remember);
submit_button = this.findViewById(R.id.submit);
// 2 設定按鈕的點擊事件
submit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 3 獲取SharedPreferences
SharedPreferences share = getSharedPreferences("my_file", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = share.edit();
// 4 保存資料到檔案
editor.putString("account", input_account.getText().toString());
editor.putString("password", input_password.getText().toString());
editor.putBoolean("pass_remem", pass_remem.isChecked()); // 單選框 選中時回傳為 true
// 5 提交資料, 并進行提示
editor.commit();
Toast.makeText(MainActivity.this, "資料寫入成功", Toast.LENGTH_SHORT).show();
// App條狀
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
// 6 如果選中,下一次加載資料
SharedPreferences sharedPreferences = getSharedPreferences("my_file", Context.MODE_PRIVATE);
Boolean pass_remem_ = sharedPreferences.getBoolean("pass_remem", false);
if (pass_remem_) {
String account = sharedPreferences.getString("account", "");
String password = sharedPreferences.getString("password", "");
input_account.setText(account);
input_password.setText(password);
pass_remem.setChecked(pass_remem_); // 恢復到原來的狀態
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/280720.html
標籤:其他
