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
PersistentStateActivity类
在本例中定义了两个文本框,对第一个文本框实现了对数据的内容和选中状态的持久化
所以当我们对文本框中的内容进行了修改,或是选中了某些文本,即使我们此时退出了程序,当我们再次打开程序时,我们依然会得到我们修改后的内容,或是某些文本的选中状态。
在第二个文本框中我们并没有实现持久化,所以当我们退出后再次进入应用时,我们之前对文本框中的内容或选中状态所做的修改就全部丢失了,文本框又恢复到了初始的状态。
这里需要注意的是: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); } } } }
相关文章推荐
- 【Android游戏特效】项目实战
- android Github-Client(ANDROID)开源之旅(三) ------ 巧用ViewPagerIndicator
- Android应用闪屏的制作,手把手教
- android 安卓开发黄金搭档:android-studio+Genymotion模拟器
- android:layout_weight 属性详解
- Android
- 【Android 多语言切换简单实例分享】
- Android5.0+(CoordinatorLayout)
- Android5.0+(CollapsingToolbarLayout)
- android5.0+(NavigationView)
- android-Fragment在生命周期做了什么?
- android-weight属性的理解
- android-当android studio更新时遇到Connetion failed。。。
- android ListActivity
- Android图像格式类及图像转换方法
- Android第一夜
- android studio 加载第三方类库
- 【Android Studio初探】快捷键大全与常用设置
- (转)Android 中的 Service 全面总结
- View类详解