【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
前给例子介绍了如何使用PreferenceActivity 来显示修改应用偏好,用户对Preferences的修改自动存储在应用对应的Shared Preferences中。
本例介绍了如何从一个Activity来取得由PreferenceActivity 的Preference值。 比如在实际应用中通过PreferenceActivity界面来取得用户偏好或是配置。
因为希望从PreferenceActivity返回值,所以使用startActivityForResult 来启动PreferenceActivity的派生类。
AdvancedPreferences 为PreferenceActivity 的派生类,它对于的R.xml.advanced_preferences 定义了两个Preferences项,一个是自定义Preference(MyPreference后面有介绍),其值类型为一整数,另一个为 CheckBoxPreference,其UI如下:

这个例子目的是通过AdvancedPreferences的设置来取得MyPreference的值,MyPreference在R.xml.advanced_preferences的定义如下:
<com.example.android.apis.preference.MyPreference
android:key="my_preference"
android:title="@string/title_my_preference"
android:summary="@string/summary_my_preference"
android:defaultValue="100" />
我们可以看到它的Key定义为my_preference。
调用AdvancedPreferences 代码如下:
// When the button is clicked, launch an activity through this intent
Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class); // Make it a subactivity so we know when it returns
startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
处理AdvancedPreferences返回:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); // The preferences returned if the request code is what we had given
// earlier in startSubActivity
if (requestCode == REQUEST_CODE_PREFERENCES) {
// Read a sample value they have set
updateCounterText();
}
} private void updateCounterText() {
// Since we're in the same package, we can use this context to get
// the default shared preferences
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0);
mCounterText.setText(getString(R.string.counter_value_is) + " " + counter);
}
可以看到取得Shared Preferences某个Preference的值是根据定义它的Key值然后调用 getXXX(key ..)来取得的。
下图则为从AdvancedPreferences 返回后在 LaunchingPreferences 显示 MyPreference的值。

【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值的更多相关文章
- 【起航计划 016】2015 起航计划 Android APIDemo的魔鬼步伐 15 App->Activity->Wallpaper 系统壁纸作为当前Activity的背景
Wallpaper介绍一个Activity如何通过Style把系统Wallpaper作为当前Activity的背景. 这是WallpaperActivity在AndroidManifest.xml中的 ...
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
App->Activity->Custom Dialog 例子使用Activity 来实现自定义对话框 类CustomDialogActivity本身无任何特别之处.关键的一点是其在And ...
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
Redirection示例涉及到三个Acitivity: RedirectEnter, RedirectMain,RedirectGetter. 示例的主Activity为 RedirectEnter ...
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
随机推荐
- 关于IBOutlet的生命周期
在调试程序的时候,发现 IBOutlet的对象在执行 getter时,开始一直想找IBOutlet对象getter方法前被执行的代码,但是一直找不到,于是我就想是不是系统自动给IBOutlet 自动初 ...
- CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation
- (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...
- json遍历,List<Map<String,Object>>遍历
js怎样给input对象追加属性,如disabled $(":textbox").attr({"disabled":true}); List<Map< ...
- php curl 请求api 接口
官方文档curl : http://www.runoob.com/php/php-ref-curl.html php开启curl : ① 打开 php.ini 文件 extension=php_c ...
- Python的安装位置与Python库
如何查看Python的安装位置: 输入 where python ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- java nio 之MappedByteBuffer
其实掌握MappedByteBuffer并不难,只要记住"三方三法三特性"(我自己总结的,呵呵~~不要扔鸡蛋哦...)这句话就可以轻松搞定!MappedByteBuffer 只是一 ...
- Python数据分析之pandas学习(基础操作)
一.pandas数据结构介绍 在pandas中有两类非常重要的数据结构,即序列Series和数据框DataFrame.Series类似于numpy中的一维数组,除了通吃一维数组可用的函数或方法,而且其 ...
- day17 isinstance type issubclass 反射
1. issubclass,type,isinstance 1.issubclass 判断xxx是否yyy的子类 例: class Foo: pass class Bar(Foo): pass cla ...
- hxml总结
段落<p> br 换行 hr 分割线   空格 > > < < & & h 7级标题 <i ...
- 【ACM】无聊的小明
无聊的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 这天小明十分无聊,没有事做,但不甘于无聊的小明聪明的想到一个解决无聊的办法,因为他突然对数的正 ...