通过preferencesActivity更改MainActivity [英] Changing MainActivity via PreferencesActivity

查看:193
本文介绍了通过preferencesActivity更改MainActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过prefernceSetting改变在MainActivity的BG。
preference.xml:

I'm trying to change BG of the MainActivity by the PrefernceSetting. Preference.xml:

...
    <CheckBoxPreference android:title="Test_bg"
            android:defaultValue="false"
            android:summary="Summary test_bg"
            android:key="test_bg" />
...

我怎么可以转移值存入MainActivity?谢谢!

How can I transfer the value into MainActivity? THANK YOU!

推荐答案

在您的活动做到这一点。

Do this in your activity

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 boolean testBg = prefs.getBoolean("test_bg", false);
 if(testBg){
      //test_bg = true
 } else {
      //test_bg = false
 }

要更改值

prefs.edit().putBoolean("test_bg", true).commit();

这篇关于通过preferencesActivity更改MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆