Android持久化数据之初识SharedPreferences

来源:互联网 发布:windows版本发布年代 编辑:程序博客网 时间:2024/05/16 03:00

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

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视图看一下保存在本地的数据。

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



0 0
原创粉丝点击