使用“首选项”的Android sharedPreferences像按钮一样工作

时间:2013-03-18 12:34:37

标签: android sharedpreferences

我使用的是共享偏好框架,但我在与其中一个首选项进行交互时遇到了一些麻烦。我想启用其中一个选项来重置我的应用。为此,我想在屏幕上显示一个确定和取消的对话框。如果用户然后单击确定我想做一堆东西。

我可以使用ImageView监听器在正常活动中编写所有这些内容。但在SharedPreferences框架中,我不知道如何添加监听器和操作。我想作为一个额外的奖励,我也需要与当前的SharedPreferences状态进行交互?

目前我有这个XML

<PreferenceCategory  android:title="Reset App">

    <Preference
        android:title="Reset App?"
        android:summary="Click here to reset the App to defaults."
        android:key="resetapp" />

</PreferenceCategory>


<PreferenceCategory  android:title="General Settings">
 <CheckBoxPreference
         android:title="Enable Sounds?"
         android:defaultValue="true"
         android:summary="A Tick here will enable sounds throughout body-mix-ology."
         android:key="enablesounds" />
 <CheckBoxPreference
         android:title="High performance Phone?"
         android:defaultValue="false"
         android:summary="If you have a high performance phone tick here to speed up body switching."
         android:key="highperformancephone" />
  </PreferenceCategory>

这是我的班级文件

public class Preferences extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preferences);

        //TODO
        // act on resetapp
        // prompt onscreen confirmation.

    }

    public AlertDialog createDialog() {

        return new AlertDialog.Builder(this)
        .setTitle("Reset App?")
        .setMessage("Are you sure? You will wipe all data from the app.")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(getBaseContext(), "App has been reset!", Toast.LENGTH_SHORT).show();
                //TODO
                // clear DB
                // reset sharedprefs.
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                Toast.makeText(getBaseContext(), "App was not reset", Toast.LENGTH_SHORT).show();

            }
        })
        .show();

   }


}

1 个答案:

答案 0 :(得分:1)

如何使用ListPreference“你确定要这样做吗?”并列出值“是”和“否”,默认为“否”。然后回复任何“是”确认,如下:

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    if (key.equals("resetapp") && prefs.getString(key,"").equals("Yes")) {
        // reset the value to "No" so that next time a preference change will be triggered
        prefs.edit().putString(key, "No").commit();

        // now do your awesome reset stuff ...
        Preference someOtherPref = findPreference(otherKey);
        ...
    }
}

不要忘记将班级定义更改为:

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

并注册eventlistener:

@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}