以下是我的问题:
- 如何获得开关首选项的值?
- 偏好片段中的内容是否已经是共享偏好?
- 如果是这样,我如何从首选项片段访问共享首选项?
以下是我的问题:
1.获取开关的值:
CompoundButton cb = (CompoundButton)view.findViewById(R.id.myswitch);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
参考:切换按钮
2.是的,a中的元素PreferenceFragment
是自动存储的。
3.要获取SharedPreference
a 的对象PreferenceFragment
,请使用:
getDefaultSharedPreferences(Context context)
这是一个关于如何制作 PreferenceFragment 的惊人示例: http ://android-er.blogspot.hu/2012/07/example-of-using-preferencefragment.html
基本上它的工作方式是您使用它们的类型和键定义首选项。首选项片段将其数据存储在您通过Android getDefaultSharedPreferences获得的 SharedPreferences 中,并编辑其值并从编辑器中获取它们。
在首选项片段中,您会自动将首选项的数据链接在片段和 SharedPrefs 之间。
另请查看此示例代码:http ://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html