【Android-数据存储】SharedPreferences的使用

android中数据存储主要分为三类:

  1. 内部存储:link.
  2. 外部存储: link.
  3. sharedpreferences

sharedpreferences也是可以说是内部存储的一种方式,但是相比于内部存储使用更加便捷,适合在保存用户偏好设置时使用。本文将介绍sharedpreferences的使用方法。

使用步骤:

  1. 拿到sharedPreferences: this.getSharedPreferences(“settings_info”, MODE_PRIVATE);
  2. 进入edit模式,拿到编辑器:SharedPreferences.Editor edit=mSharedPreferences.edit();
  3. 保存数据【有多种数据类型可以保存:boolean,string,int,float等】:edit.putBoolean(“state”,isChecked);//以键值对的形式保存/读取数据
  4. 提交编辑器:edit.commit();

sharedpreferences的存储路径:/data/data/shared_prefs目录下

——————————————————————————————————————————

案例1:用户偏好设置

效果:更改了选项后再次回到该界面不会恢复默认值
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="未知来源"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"
            android:padding="20dp"/>

        <TextView
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="允许安装未知来源的应用"
            android:textSize="18sp"/>

    </LinearLayout>

    <Switch
        android:id="@+id/is_allow_unknown_apps_sources_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_marginRight="10dp"/>

</RelativeLayout>
public class PreferenceDemoActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private static final String TAG ="PreferenceDemoActivity" ;
    private Switch mIsAllowUnknownSource;
    private SharedPreferences mSharedPreferences;

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

        mIsAllowUnknownSource = findViewById(R.id.is_allow_unknown_apps_sources_switch);
        mIsAllowUnknownSource.setOnCheckedChangeListener(this); //当选择改变时的监听
        mSharedPreferences = PreferenceDemoActivity.this.getSharedPreferences("settings_info", MODE_PRIVATE);

        //回显数据,默认为false
        boolean state = mSharedPreferences.getBoolean("state", false);
        mIsAllowUnknownSource.setChecked(state);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //假设说在这里需要对数据进行保存,并设置回显
        Log.d(TAG,"current state is "+isChecked);
        SharedPreferences.Editor edit=mSharedPreferences.edit();
        edit.putBoolean("state",isChecked);
        edit.commit();
    }
}

文件路径:
在这里插入图片描述内容:
在这里插入图片描述

案例2:数据存储、读取展示

存数据时使用:put
读数据时使用:set

效果:输入完内容后可以将数据内容展示,可设置,在退出应用程序后再次打开仍回显数据(使用场景:登录时的记住密码,记住账号等功能)

在这里插入图片描述

<?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">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input message"
        android:textSize="20sp"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="存数据"/>

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读数据"/>


    <TextView
        android:id="@+id/tv_show_message"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#60999999"
        android:textSize="20sp"/>

</LinearLayout>
public class SharedActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG ="SharedActivity" ;
    private Button mBtnWrite;
    private Button mBtnRead;
    private EditText mEtMessage;
    private TextView mTvShow;
    private SharedPreferences mSharedPreferences;

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

        initView();
        initEvent();
        mSharedPreferences = this.getSharedPreferences("message_info", MODE_PRIVATE);
        mTvShow.setText(mSharedPreferences.getString("message","null"));
    }

    private void initEvent() {
        mBtnWrite.setOnClickListener(this);
        mBtnRead.setOnClickListener(this);
    }

    private void initView() {
        mBtnWrite = findViewById(R.id.btn_save);
        mBtnRead = findViewById(R.id.btn_read);
        mEtMessage = findViewById(R.id.et_input);
        mTvShow = findViewById(R.id.tv_show_message);
    }


    @Override
    public void onClick(View v) {
        if (v == mBtnWrite){
            Log.d(TAG,"write message");
            String message = mEtMessage.getText().toString();
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString("message",message);
            edit.commit();
        }else if (v == mBtnRead){
            Log.d(TAG,"read message...");
            mTvShow.setText(mSharedPreferences.getString("message","null"));
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值