首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EditTextPreference禁用按钮?

EditTextPreference禁用按钮?
EN

Stack Overflow用户
提问于 2011-01-22 01:46:47
回答 2查看 4.2K关注 0票数 4

我想有一个在EditText字段中没有文本时禁用OK按钮的EditTextPreference。我创建了一个自定义EditTextPreference类,可以获取EditText对象并设置TextWatcher,但我找不到禁用该按钮的方法。看起来我只是无法访问对话框中的“确定”和“取消”按钮。

有人知道如何获得这些按钮或做我想做的事吗?

唯一的其他选择是尝试从头开始创建一个与EditTextPreference相似的自定义对话框。

EN

回答 2

Stack Overflow用户

发布于 2011-01-22 03:34:51

下面是一个代码示例,根据onCheckValue函数返回的是true还是false来启用/禁用按钮。

代码语言:javascript
复制
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();
    }    
}
票数 9
EN

Stack Overflow用户

发布于 2013-07-22 18:00:48

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

我做了以下工作:

旧代码:

代码语言:javascript
复制
<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"/>

新代码:

代码语言:javascript
复制
<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"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4762027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档