首页 > 解决方案 > 检测 EditTextPreference 上的负按钮按下

问题描述

我有一个带有 EditTextPreference 的 PreferenceActivity。我可以很容易地检测到积极的按钮按下使用OnPreferenceChangedListener。但是,当用户按下否定按钮时,永远不会调用侦听器。

反正有检测吗?谢谢。

标签: androidpreferenceactivity

解决方案


onDialogClosed(...)除非您覆盖in EditTextPreference(假设您使用旧的且现已弃用的框架版本的首选项),否则您无法检测到它:

public class EditTextPreferenceNegative extends EditTextPreference {
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult); // this will notify the change listener for positive results

        if (!positiveResult) {
            // do something with your negative result
        }
    }
}

但是请注意,即使对话框在没有按下否定按钮的情况下关闭,也会调用它。如果您只想检测否定按钮,您可以覆盖onClick(...)单击任一按钮时调用的方法。


推荐阅读