Android开发笔记(二十一)——数据存储之SharedPreference轻量级数据存储

  • xml文件,K-V形式
  • SharedPreferences:读取数据
  • SharedPreferences.Editor:写入数据

代码示例

在EditText中输入内容,点击保存,通过SharedPreferences来保存数据,点击显示按钮,把数据读取出来,显示在下面的TextView上。

SharedPreferencesActivity的java代码:

public class SharedPreferencesActivity extends AppCompatActivity {

    private EditText mEtName;
    private Button mBtnSave,mBtnShow;
    private TextView mTvContent;
    
    //读取数据
    private SharedPreferences mSharedPreferences;
    //写入数据
    private SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);

        mEtName=findViewById(R.id.et_name);
        mBtnSave=findViewById(R.id.btn_save);
        mBtnShow=findViewById(R.id.btn_show);
        mTvContent=findViewById(R.id.tv_content);

        //实例化mSharedPreferences,参数:文件名称,模式(通常使用PRIVATE表示当前文件只有本应用可以读写)
        mSharedPreferences = getSharedPreferences("data",MODE_PRIVATE); 
        //实例化mEditor
        mEditor = mSharedPreferences.edit();

        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现把EditText的文本通过SharedPreferences保存起来
                //putString写入数据:键值对,参数:key,value
                mEditor.putString("name",mEtName.getText().toString()); //mEtName.getText().toString()获取EditText文本的内容
                mEditor.apply();
            }
        });

        mBtnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //getString读出数据,参数:key,default(缺省的值,设为空即可)
                mTvContent.setText(mSharedPreferences.getString("name",""));
            }
        });
    }
}

对应的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容"
        />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:layout_marginTop="10dp"
        />

    <Button
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"
        android:layout_marginTop="10dp"
        />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />

</LinearLayout>

运行效果

文件保存位置:

在Android系统中,其配置文件的数据文件 以XML文件的形式保存在:/data/data/<applicationId>/shard_prefs

<applicationId> 默认是包名,但其本质不是包名

这个data.xml文件就是通过SharedPreference存储之后生成的一个xml文件

总结

//定义读写对象
SharedPreferences sharedPreferences = getSharedPreferences(“参数名”,MODE_PRIVATE)
SharedPreferences.Editor editor = sharedPreferences.edit()
//写入数据
editor.putString(“参数名”,editText.getText().toString())
editor.apply();
//读出数据
sharedPreferences.getString(“参数名”,"")
原文地址:https://www.cnblogs.com/yangdd/p/13373015.html