OnPreferenceClickListener无法正常工作

时间:2016-05-05 19:31:52

标签: android

我想在Preferences XML中将onClickListener设置为我的Checkbox。

当我想设置onClickListener时,它不起作用。 在“设置活动”中, setOnPreferenceClickListener 上出现红色错误。

有人帮忙吗?感谢

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="@string/Movie_feed"
    android:key="pref_overall"
    >

<CheckBoxPreference
    android:id="@+id/TopRated"
    android:key="pref_rated"
    android:summary="@string/Highest_rated"
    android:defaultValue="true" />
<CheckBoxPreference
    android:id="@+id/TopPopular"
    android:key="pref_popular"
    android:summary="@string/Most_popular"
    android:defaultValue="true" />
</PreferenceCategory>

public class SettingsActivity extends PreferenceActivity {

private CheckBox boxPopular;
private CheckBoxPreference boxTopRated;


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



    final CheckBox boxPopular = (CheckBox) findViewById(R.id.TopPopular);
    boxPopular.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            return false;
        }
    });

1 个答案:

答案 0 :(得分:1)

尝试findPreference() + setOnPreferenceChangeListener()

findPreference("pref_popular").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return true;
    }
});