我有一个首选项类,允许用户从应用程序的设置中打开“提示”:
public class Prefs extends PreferenceActivity {
//Option names and defualt values
private static final String OPT_HINTS = "hints";
private static final boolean OPT_HINTS_DEF = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
/**Get the current value of the hints option */
public static boolean getHints(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_HINTS, OPT_HINTS_DEF);
}
}
xml文件包含:
<CheckBoxPreference
android:key="hints"
android:title="@string/hints_title"
android:summary="@string/hints_summary"
android:defaultValue="true" />
如何检查'提示'是否设置为开。然后,如果它设置为On,我想允许应用程序做某件事。
感谢您的帮助
答案 0 :(得分:0)
如何检查'提示'是否设置为开。然后,如果它设置为On,我想允许应用程序做某件事。
使用您的代码只需致电:
if(getHints(this)) {
// The preference is checked, do something
}
else {
// The preference isn't checked
}
虽然在添加更多首选项时可能更容易在类变量中保存PreferenceManager.getDefaultSharedPreferences(context)
。