如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPi

How to use setOnPreferenceChangeListener for QuietlyCoding NumberPicker?(如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?)
本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How To Create a Rotating Wheel Control?(如何创建转轮控件?)
How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))
iOS 8 Rotation Methods Deprecation - Backwards Compatibility(iOS 8 旋转方法弃用 - 向后兼容性)
Screen orientation lock(屏幕方向锁定)
Strange behavior with android orientation sensor(android方向传感器的奇怪行为)