如何检查/通过设置状态

时间:2013-02-27 18:18:45

标签: java android

我有一个首选项类,允许用户从应用程序的设置中打开“提示”:

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,我想允许应用程序做某件事。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

  

如何检查'提示'是否设置为开。然后,如果它设置为On,我想允许应用程序做某件事。

使用您的代码只需致电:

if(getHints(this)) {
    // The preference is checked, do something
}
else {
    // The preference isn't checked
}

虽然在添加更多首选项时可能更容易在类变量中保存PreferenceManager.getDefaultSharedPreferences(context)

相关问题