您的位置:首页 > 移动开发 > Android开发

Android Api Demos登顶之路(十一)Persistent state

2015-07-28 06:56 453 查看
这个demo演示了利用 SharedPreferences进行数据的持久化。

在本例中定义了两个文本框,对第一个文本框实现了对数据的内容和选中状态的持久化

所以当我们对文本框中的内容进行了修改,或是选中了某些文本,即使我们此时退出了程序,当我们再次打开程序时,我们依然会得到我们修改后的内容,或是某些文本的选中状态。

在第二个文本框中我们并没有实现持久化,所以当我们退出后再次进入应用时,我们之前对文本框中的内容或选中状态所做的修改就全部丢失了,文本框又恢复到了初始的状态。

这里需要注意的是:SharedPreferences 类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。

获取SharedPreferences对象方法:有三种

1.SharedPreferences pre = Context.getSharedPreferences(String name,int mode);

2.SharedPreferences pre = Activity.getPreferences(int mode);

3.SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(Context);

本例使用的是第二种。

布局文件:activity_persistent_state.xml

<?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:padding="5dp"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Demonstration of persistent activity state with getPreferneces(0).edit() and getPreferneces(0)."/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="10dp"
android:text="This text field save its state"/>
<EditText
android:id="@+id/et_persistent_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:text="Initial text"
android:background="#00ff00"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="10dp"
android:text="This text field does not save its state"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:text="Initial text"
android:background="#ff0000"/>
</LinearLayout>


PersistentStateActivity类

public class PersistentStateActivity extends Activity {
private EditText et_save;
private SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_persistent_state);
et_save = (EditText) findViewById(R.id.et_persistent_save);
//MODE_PRIVATE: 私有方式存储,其他应用无法访问
sp = getPreferences(MODE_PRIVATE);
}

/**
* 当activity不可见时保存EditText的状态
*/
@Override
protected void onPause() {
super.onPause();
// 获取SharedPreferences的编辑器
Editor editor = sp.edit();
// 向SharedPreferences中存储文本和选中的状态
editor.putString("text", et_save.getText().toString());
editor.putInt("selection_start", et_save.getSelectionStart());
editor.putInt("selection_end", et_save.getSelectionEnd());
// 提交数据,注意存储完成后一定要注意提交数据,否则数据不会被保存到SharedPreferences当中
editor.commit();
}

/**
* 当activity恢复时恢复EditText的状态
*/

@Override
protected void onResume() {
super.onResume();
// 从SharedPreferences中取出我们存入的文本和选中状态信息
String text = sp.getString("text", null);
if (text!= null && text!="") {
//TextView.BufferType.EDITABLE允许在原来显示的文件的基础上追加文本,其实默认的状态下就可以
//追加文本,即使我们不设置这个参数
et_save.setText(text,TextView.BufferType.EDITABLE);
// 设置默认值为-1
int start = sp.getInt("selection_start", -1);
int end = sp.getInt("selection_end", -1);
if (start != -1 && end != -1) {
et_save.setSelection(start, end);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: