SharePreferences

11625 ワード

  • SharePreferencesとは何ですか
  • データの格納方法
  • データの読み出し方法
  • ケース–ログインページ記憶パスワード

    SharePreferencesって何?

    SharedPreferencesはAndroidプラットフォーム上の軽量レベルのストレージクラスであり、Activityステータス、Activity一時停止時にSharedPereferencesに保存するなど、アプリケーションの一般的な構成を保存するために使用されます.Activityがリロードされ、システムコールバックメソッドonSaveInstanceStateが使用されると、SharedPreferencesから値が取り出されます.

    ストレージデータ

    SharedPreferenceオブジェクトを取得すると、SharedPreferenceファイルへのデータの格納を開始できます.主に3つのステップがあります.
  • SharedPreferenceオブジェクトのedit()メソッドを使用してSharedPreferenceを取得する.Editorオブジェクト
  • SharedPreference.Editorオブジェクトにデータを追加します.たとえば、ブール型のデータを追加するのはputBooleanメソッドで、文字列を追加するのはputString()メソッドで、
  • を押します.
  • commit()メソッドを呼び出して追加データをコミットし、データ格納動作
  • を完了する.
    private SharePreferences sp;
    
    // SharePreferences , XML demo_01, MODE_PRIVATE
    
    sp=this.getSharedPreferences("demo_01",MODE_PRIVATE);
    
    // edit() SharePreferences.Editor 
    SharePreferences.Editor editor =sp.edit();
    // putString() , 
    editor.putString("name","sssss");
    
    // commit() 
    
    editor.commit();

    データの読み込み

    SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
    String name = sp.getString("name", null);
    int age = sp.getInt("age", 0);

    ケース

    public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
        private EditText name_ed, psw_ed;
        private Button login_btn, reg_btn;
        private CheckBox checkBox;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            bindID();
    
            SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
            int checked = sharedPreferences.getInt("checked", 0);
    
            if (checked == 1) {
                String name = sharedPreferences.getString("username", "");
                String psw = sharedPreferences.getString("psw", "");
                name_ed.setText(name);
                psw_ed.setText(psw);
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
        }
    
        private void bindID() {
            name_ed = findViewById(R.id.ed1);
            psw_ed = findViewById(R.id.ed2);
            login_btn = findViewById(R.id.btn1);
            reg_btn = findViewById(R.id.btn2);
            checkBox = findViewById(R.id.cb);
            login_btn.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn1:
                    SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    if (checkBox.isChecked()) {
                        String username = name_ed.getText().toString();
                        String password = psw_ed.getText().toString();
                        editor.putString("username", username);
                        editor.putString("psw", password);
                        editor.putInt("checked", 1);
                    } else {
                        editor.putString("username", "");
                        editor.putString("psw", "");
                        editor.putInt("checked", 0);
                    }
                    editor.commit();
    
                    break;
    
            }
        }
    }
    <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=".Main2Activity">
    
        <EditText
            android:id="@+id/ed1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <EditText
            android:id="@+id/ed2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">
    
                <Button
                    android:id="@+id/btn1"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:text=" " />
    
                <Button
                    android:id="@+id/btn2"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/btn1"
                    android:text=" " />
            LinearLayout>
    
        RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text=" "
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    RelativeLayout>
    LinearLayout>