2015-07-04 32 views
0

我试图通过PrefernceSetting更改MainActivity的BG。 Preference.xml:通过PreferencesActivity更改MainActivity

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

我怎么能值转移到MainActivity?谢谢!

回答

0

在您的活动做这个

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(); 
+0

谢谢!但我不明白:('SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs.getBoolean(“test_bg”,false);' - 我可以放入OnCreate方法吗?这个样本:RelativeLayout.setBackgroundColor(getResources()。getColor(R.color.redColor));我认为algorythm需要这样: –

+0

If(“test_bg”== true)FrameLayout.setBackgroundColor(getResources()。 getColor(R.color.redColor)); else RelativeLayout.setBackgroundColor(getResources()。getColor(R.color.blackColor)); –

+0

看到我编辑的答案 – Emil