前给例子介绍了如何使用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中的值的更多相关文章

  1. 【起航计划 016】2015 起航计划 Android APIDemo的魔鬼步伐 15 App->Activity->Wallpaper 系统壁纸作为当前Activity的背景

    Wallpaper介绍一个Activity如何通过Style把系统Wallpaper作为当前Activity的背景. 这是WallpaperActivity在AndroidManifest.xml中的 ...

  2. 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用

    App->Activity->Custom Dialog 例子使用Activity 来实现自定义对话框 类CustomDialogActivity本身无任何特别之处.关键的一点是其在And ...

  3. 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect

    Redirection示例涉及到三个Acitivity: RedirectEnter, RedirectMain,RedirectGetter. 示例的主Activity为 RedirectEnter ...

  4. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  5. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  6. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  7. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  8. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  9. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

随机推荐

  1. 2019年GPLT L2-3 深入虎穴 比赛题解 中国高校计算机大赛-团体程序设计天梯赛题解

    著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或者又有很多条路,同样是每条 ...

  2. CF1101A Minimum Integer 模拟

    题意翻译 题意简述 给出qqq组询问,每组询问给出l,r,dl,r,dl,r,d,求一个最小的正整数xxx满足d∣x d | x\ d∣x 且x̸∈[l,r] x \not\in [l,r]x̸∈[l ...

  3. CF D Bicolorings

    题意 给一个2行n列的矩阵填上黑色和白色,求连通块个数为k个的填色方案数量(mod 998244353)   因为只有两行,为n-1列的矩阵增加1列的情况数只有很少,容易想到用 (i,k) 表示 i  ...

  4. Jenkins自动化CI CD流水线之4--Master-Slave架构

    一.介绍 jenkins的Master-slave分布式架构主要是为了解决jenkins单点构建任务多.负载较高.性能不足的场景. Master/Slave相当于Server和agent的概念.Mas ...

  5. Spring Boot中JPA如何实现按日期合计

    1. 用queryDsl方法 JPAQueryFactory.select( Projections.fields(OrderCountByDayBean.class, qOrder.amount.s ...

  6. ymPrompt.js消息提示组件

    转载:https://www.cnblogs.com/linzheng/archive/2010/11/15/1878058.html 使用说明: 1.在页面中引入ymPrompt.js.如:< ...

  7. Django重新整理3

    Forms组件 1.在models.py中我们建立一个新的表关系: class UserInfo(models.Model): user=models.CharField(max_length=32) ...

  8. ubuntu 16.04安装后不能登入

    启动后,选择ubuntu高级选项,选择恢复模式,在恢复模式下 sudo apt-get update sudo apt-get upgrade 另外,可以在此模式下,选择nvidia驱动

  9. 初识contiki(2.7版本)

    一个偶然的机会,我接触到了contiki这个家伙. Contiki 是一个开源的.高度可移植的.采用 C 语言开发的非常小型的嵌入式操作系统,针对小内存微控制器设计,适用于联网嵌入式系统和无线传感器网 ...

  10. SQL命令行操作

    命令行操作(mysql.exe)    0.登录  :       mysql -u root -p    1.显示数据库列表:    show databases;     2.选择数据库:     ...