码迷,mamicode.com
首页 > 移动开发 > 详细

android学习十三 首选项

时间:2016-08-23 18:41:12      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

1,首选项可用用来持久保存用户设置,游戏最高分等

2,首选项有,列表首选项,复选框首选项,对话框首选项..
3,通过xml文件和代码创建首选项
     addPreferencesFromResource(R.xml.main)
4.从首选项中获取值
   Resources resources = this.getResources();
              SharedPreferences spf= PreferenceManager.getDefaultSharedPreferences( this);
              String option = spf.getString(resources.getString(R.string.FLIGHT_SORT_KEY),
                            resources.getString(R.string.FLIGHT_SORT_DEFAULT_VALUE ));
              
              String[] optionArray = resources.getStringArray(R.array.filght_sort_option );
5.判断是否存在某个首选项的值
 
boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false);
if(hasPreferences) {
Log.v("Preferences", "We‘ve been called before");
// Read other values as desired from preferences file...
someString = myPrefs.getString("someString", "");
}
else {
Log.v("Preferences", "First time ever being called");
// Set up initial values for what will end up
// in the preferences file
someString = "some default value";
}
// Later when ready to write out values
 
6.在代码中设置首选项的值
Editor editor = myPrefs.edit();
CHAPTER 13: Working with Preferences and Saving State  359
editor.putBoolean(INITIALIZED, true);
editor.putString("someString", someString);
// Write other values as desired
editor.commit();
 
 
7.首选项可用嵌套,可以存在父子关系。
 
 
 
 
8一般步骤
 
  a.在xml文件中布局首选项
  b.创建活动继承自PreferenceActivity,然后通过
   addPreferencesFromResource() 创建视图
  c.通过相应的方法获取和设置值
   

android学习十三 首选项

标签:

原文地址:http://www.cnblogs.com/manziluo/p/5800466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!