如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPi

时间:2023-04-24
本文介绍了如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想检测 NumberPicker 中的值何时更改.我的 PreferenceActivity 上有这段代码:

I want to detect when the value from the NumberPicker is changed. I have this code on my PreferenceActivity:

public class MainPrefs extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.xml.main_preferences);
        this.findPreference("SMSSentLimit").setOnPreferenceChangeListener(
            new OnPreferenceChangeListener() {

                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    TrackerService.updateStats(Long.decode(newValue.toString()));
                    return true;
                }
            });
        this.findPreference("NumberPickerLimit").setOnPreferenceChangeListener(
            new Preference.OnPreferenceChangeListener() {

                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    Log.i("onPreferenceChange", "NumberPicker Changed");
                    Toast.makeText(getBaseContext(), "CHANGEEEED !!!",
                        Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
    }
}

第二个 (findPreference("NumberPickerLimit")) 是 NumberPicker,如果我将其更改为 onPreferenceClickListener,它永远不会被调用它可以工作,但是当我单击首选项而不是更改值时,我会检测到.

The second one (findPreference("NumberPickerLimit")) is the NumberPicker and it is never called, if I change it to onPreferenceClickListener it works but I detect when I click the preference instead when I change the value.

根据源代码应该是这样调用的:

Acording to source code it should be called:

public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                saveValue(mPicker.getCurrent());
                break;
            default:
                break;
        }
    }

private void saveValue(int val) {
    getEditor().putInt(getKey(), val).commit();
    notifyChanged();
}

发生了什么事?是bug吗?

Whats happening? Is it a bug?

编辑这是我的 XML:

<com.michaelnovakjr.numberpicker.NumberPickerPreference
            android:key="NumberPickerLimit"
            android:title="@string/NumberPickerTitle"
            android:summary="@string/NumberPickerSummary"
            picker:defaultValue="1"
            picker:startRange="1"
            picker:endRange="31" />

推荐答案

刚刚查看了您列出的 xml 文件 这里,看起来你有 android:key="demo.preference" 那里.而在此处的代码中,您使用的是 findPreference("NumberPickerLimit").

Just looked at your xml file listed here, looks like you have android:key="demo.preference" there. Whereas in the code here, you're using findPreference("NumberPickerLimit").

这篇关于如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:对话框android上的轮子选择器 下一篇:MapView 在对话框中显示较暗的地图

相关文章