SharedPreferences を使用してキーと値のペアのデータを保存する Android 開発

SharedPreferences を使用してキーと値のペアのデータを保存する

保持したいキーと値のペアの比較的小さなコレクションがある場合は、SharedPreferences API を使用する必要があります。SharedPreferences オブジェクトは、キーと値のペアを含むファイルを指し、それらのキーと値のペアを読み書きするための簡単なメソッドを提供します。各 SharedPreferences ファイルはフレームワークによって管理され、プライベートにすることも共有することもできます。

注: SharedPreferences API は、キーと値のペアの読み取りと書き込みの機能のみを提供します。Preference API と混同しないでください
。これらは同じものではありません。後者は、ユーザー プロファイルを設定する UI を作成するのに役立ちます (ただし、SharedPreferences はアプリ設定の保存にも使用されます)。

AndroidManifest.xml

 <activity android:name=".SaveData">
        <intent-filter>
            <action android:name="sp" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

保存データ.xml

ここに画像の説明を挿入

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        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:layout_marginTop="30dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_input"
                android:layout_width="287dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="30dp"
                android:layout_weight="1"
                android:textSize="20dp"></EditText>

            <Button
                android:id="@+id/btn_saveContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:onClick="save"
                android:text="存入SHAREDPREFENCE"
                android:textColor="#090909"
                android:textSize="20dp"
                app:backgroundTint="#E3E2E2"></Button>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="30dp">

            <TextView
                android:id="@+id/tv_result"
                android:layout_width="332dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:textSize="20dp"></TextView>

            <Button
                android:id="@+id/btn_showContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:textColor="#090909"
                android:text="从SHAREDPREFERENCE取出"
                app:backgroundTint="#E3E2E2"
                android:textSize="20dp"></Button>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

保存されました

  1. Shared Preferences Write へのハンドルを取得する
  2. 共有設定
  3. 共有設定ファイルからデータを読み取る
package com.example.myapplication4;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SaveData extends AppCompatActivity implements View.OnClickListener {
    
    
    private EditText et_content;
    private TextView tv_content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_savedata);
        findViewById(R.id.btn_saveContent).setOnClickListener(this);
        findViewById(R.id.btn_showContent).setOnClickListener(this);
        tv_content = (TextView) findViewById(R.id.tv_result);
        et_content = (EditText) findViewById(R.id.et_input);
        if(tv_content.getText().toString().trim().equals("")){
    
    
            SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
            tv_content.setText(sp.getString("content", ""));
            et_content.setText(sp.getString("content", ""));
        }
    }


    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()) {
    
    

            case R.id.btn_saveContent:
                SharedPreferences sp = this.getSharedPreferences("data", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("content", et_content.getText().toString().trim());
                editor.commit();
                Toast.makeText(this, "存入成功", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_showContent:
                sp = this.getSharedPreferences("data", MODE_PRIVATE);
                tv_content.setText(sp.getString("content", ""));
                Toast.makeText(this, "取出成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }




}

実験結果

ここに画像の説明を挿入

要約する

SharedPreferences オブジェクトを取得するには、次の 2 つのメソッドを使用します。

getSharedPreferences() – このメソッドの最初のパラメータは環境設定ファイル名であり、このメソッドは複数の異なる環境設定ファイルを区別するために使用されます;
getPreferences() – アクティビティで 1 つの環境設定ファイルのみを使用する場合、このメソッドにはファイル名は必要ありません。

書き込み値:

edit() を呼び出して SharedPreferences.Editor オブジェクトを取得し、エディタ オブジェクトの putXXX() メソッドを使用して値を書き込み、最後に commit() メソッドを通じてデータ変更全体を送信します。

おすすめ

転載: blog.csdn.net/qq_46556714/article/details/126580932