Android持久化数据之初识SharedPreferences

本文介绍了一种在Android应用中实现数据持久化的简单方法,通过SharedPreferences保存用户设置,包括开关状态、复选框和单选按钮的选择情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我们开发程序过程中,经常会和数据打交道,一些数据在你关闭程序之后就不复存在了,因为他们被存储于内存当中。但是有时候我们希望数据能够长期存储在设备上,这就需要使用到持久化技术。持久化技术通过将数据存在本地而不会因为关闭程序而丢失。比如用户在设置页面开启了某个选项,当离开设置页面的时候,用户的操作应当被保留下来,不然会造成不良的用户体验。接下去通过代码来学习一下如何实现。

1.首先新建一个布局文件:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="设置"/>
        </RelativeLayout>

代码非常简单,仅仅放置一个设置按钮。

 

2.在新建一个setting布局:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_centerVertical="true"
            android:text="para1"/>
        <Switch
            android:id="@+id/swc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
        </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:text="para2"/>
        <CheckBox
            android:id="@+id/ckbx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_centerVertical="true"
            android:text="para3"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true">
            <RadioButton
                android:id="@+id/rdbt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/rdbt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/rdbt1"/>
            </RadioGroup>
</RelativeLayout>

在Setting布局中我们加入了一个switch,一个checkbox,一个radiobox。

 

3.修改MainActivity:

public class MainActivity extends Activity {
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn= (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Setting.class);
                startActivity(intent);
            }
        });
    }
}

我们对按钮增加监听事件,使其跳转至Setting页面。

 

4.在Setting类中实现SharedPreferences:

public class Setting extends Activity {
    private Switch aSwitch;
    private CheckBox aCheckBox;
    private RadioButton aRadioButton;
    private RadioButton bRadioButton;

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        aSwitch= (Switch) findViewById(R.id.swc);
        aCheckBox= (CheckBox) findViewById(R.id.ckbx);
        aRadioButton= (RadioButton) findViewById(R.id.rdbt1);
        bRadioButton= (RadioButton) findViewById(R.id.rdbt2);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        //从SharedPreferences中读取数据,默认不选中
        boolean switchRemember=pref.getBoolean("switch_remember",false);
        boolean checkRemember=pref.getBoolean("check_remember",false);
        boolean radioRemember1=pref.getBoolean("radio1_remember",false);
        boolean radioRemember2=pref.getBoolean("radio2_remember",false);
        //如果读取到开关为选中,则设置开关为选中
        if(switchRemember){
            aSwitch.setChecked(true);
        }
        if(checkRemember){
            aCheckBox.setChecked(true);
        }
        if(radioRemember1){
            aRadioButton.setChecked(true);
        }
        if(radioRemember2){
            bRadioButton.setChecked(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象
        editor=pref.edit();
        if(aSwitch.isChecked()){
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("switch_remember",true);
        }else {
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("switch_remember",false);
        }

        //检查开关是否被选中
        if(aCheckBox.isChecked()){
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("check_remember",true);
        }else {
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("check_remember",false);
        }

        //检查开关是否被选中
        if(aRadioButton.isChecked()){
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("radio1_remember",true);
        }else {
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("radio1_remember",false);
        }
        //检查开关是否被选中
        if(bRadioButton.isChecked()){
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("radio2_remember",true);
        }else {
            //向SharedPreferences.Editor对象中添加数据
            editor.putBoolean("radio2_remember",false);
        }
        //提交数据
        editor.commit();
    }
}
当在Setting页面按下Back之后,用户的操作将被保存在本地。

下面我们通过DDMS视图看一下保存在本地的数据。

将其导出至本地,双击打开我们能看到被保存下来的数据:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值