2

我正在尝试在我的 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;
    }

}
4

2 回答 2

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(......
}
}
于 2013-05-08T20:28:15.070 回答
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;
    }
于 2015-08-04T08:39:01.020 回答