onPreferenceClick和OnPreferenceClickListener

时间:2013-05-08 20:13:13

标签: android

我正在尝试在我的java代码中评估我的首选项,以启用/禁用他们选择不执行其他选项的其他选项...到目前为止,我只是尝试实现OnPreferenceClickListener但是我从未看到过从变化中敬酒。我究竟做错了什么?似乎有很多其他类似的问题,但我无法看到我的错误。

public class UserSettingActivity extends PreferenceActivity implements OnPreferenceClickListener{
    SharedPreferences mPreferences;
    Boolean frequency;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }


    @Override
    public boolean onPreferenceClick(Preference preference) {
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        frequency = mPreferences.getBoolean("frequency", true);

        Context context = getApplicationContext();
        Toast.makeText(context, "Hello toast 0!", Toast.LENGTH_LONG).show();

        if (!frequency) {
            Context context2 = getApplicationContext();
            Toast.makeText(context2, "Hello toast 1!", Toast.LENGTH_LONG).show();

        } else if (preference.getKey().equals("schedulestop")) {

        } else if (preference.getKey().equals("priority")) {

        } else {
            Context context3 = getApplicationContext();
            Toast.makeText(context3, "Hello toast 0!", Toast.LENGTH_LONG).show();
        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:4)

您必须为每个个人偏好注册PreferenceClickListener

somePreference.setOnPreferenceClickListener(this);  

或者您可以将getSharedPreferences().registerOnSharedPreferenceChangeListener用于所有偏好设置。

public class UserSettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
SharedPreferences mPreferences;
Boolean frequency;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
}

@SuppressWarnings("deprecation")
@Override
protected void onPause()
{
    super.onPause();

    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@SuppressWarnings("deprecation")
@Override
protected void onResume()
{
    super.onResume();

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}


@SuppressWarnings("deprecation")
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("schedulestop")) {
         // do something
     }
     else if (key.equals(......
}
}

答案 1 :(得分:1)

首先实施" OnSharedPreferenceChangeListener"

PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener

,然后

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    settings.registerOnSharedPreferenceChangeListener(this);

}

,然后

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    switch (key) {
        case "pref_key_auto_delete":
            boolean notificationStatus = SP.getBoolean(key, false);
            Log.d("stat", String.valueOf(notificationStatus));
            break;

        case "pref_key_notification_list":
            String downloadType = SP.getString(key, "n/a");
            Log.d("stat", String.valueOf(downloadType));
            break;
    }