EditTextPreference禁用按钮?

4
我想要一个EditTextPreference,如果EditText字段中没有文本,则禁用OK按钮。我创建了一个自定义的EditTextPreference类,能够获取EditText对象并设置TextWatcher,但是我找不到一种方法来禁用按钮。看起来我无法访问对话框中的OK和Cancel按钮。
有人知道如何获取这些按钮或者实现我想要的功能吗?
唯一的其他选择是尝试从头开始创建自定义对话框,使其看起来和模仿EditTextPreference。

你得到解决方案了吗? - Rishabh Srivastava
2个回答

9
这里有一段代码示例,根据onCheckValue函数返回的结果是true还是false来启用/禁用按钮。请看以下内容:

public class ValidatedEditTextPreference extends EditTextPreference
{
    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs, int defStyle)
    {
        super(ctx, attrs, defStyle);        
    }

    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);                
    }

    private class EditTextWatcher implements TextWatcher
    {    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){}

        @Override
        public void beforeTextChanged(CharSequence s, int start, int before, int count){}

        @Override
        public void afterTextChanged(Editable s)
        {        
            onEditTextChanged();
        }
    }
    EditTextWatcher m_watcher = new EditTextWatcher();

    /**
     * Return true in order to enable positive button or false to disable it.
     */
    protected boolean onCheckValue(String value)
    {        
        return Strings.hasValue(value);
    }

    protected void onEditTextChanged()
    {
        boolean enable = onCheckValue(getEditText().getText().toString());
        Dialog dlg = getDialog();
        if(dlg instanceof AlertDialog)
        {
            AlertDialog alertDlg = (AlertDialog)dlg;
            Button btn = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);
            btn.setEnabled(enable);                
        }
    }

    @Override
    protected void showDialog(Bundle state)
    {
        super.showDialog(state);

        getEditText().removeTextChangedListener(m_watcher);
        getEditText().addTextChangedListener(m_watcher);
        onEditTextChanged();
    }    
}

我仍然不知道如何访问这些按钮。我需要能够禁用“确定”按钮。你所建议的只是让我自己创建对话框。如果我采取这种方法,我将只构建一个自定义的Dialog类。 - Bob
我已经更新了答案。我已经自己检查了这种新方法,对我有效。 - inazaruk

2

需要将preference.xml中旧的EditTextPreference更改为新的ValidatedEditTextPreference。

我做了以下修改:

旧代码:

<EditTextPreference
    android:dialogMessage="Please input your email"
    android:dialogTitle="Set email"
    android:key="Email"
    android:summary="Your email"
    android:title="-Missed call send to" android:defaultValue="xxx@xxx.xxx"/>

新代码:
<com.tanggod.missedcall2mail.ValidatedEditTextPreference
    android:dialogMessage="Please input your email"
    android:dialogTitle="Set email"
    android:key="Email"
    android:summary="Your email"
    android:title="-Missed call send to" android:defaultValue="xxx@xxx.xxx"/>

1
他为什么需要改用新的ValidatedEditTextPreference?显然,它不是API的一部分,因为你需要添加其他库才能使其工作。此外,你正在回答两年前提出的问题,所以Bob可能并不需要针对这个问题的答案。 - BLaZuRE
是的,你说得对,我才发现那是2年前的事情。因为我按照inazaruk的方法将EditTextPreference修改为ValidatedEditTextPreference。我只是演示了如何使用它。 - DàChún

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接