的EditText preference禁用按钮? [英] EditTextPreference Disable Buttons?
问题描述
我想有一个EditText preference将禁用确定按钮,如果有在现场的EditText没有文本。我创建了一个自定义的EditText preference类,我能够得到的EditText对象,并设置一个TextWatcher,但我不能找到一种方法来禁用该按钮。它看起来像我只是没有获得确定和取消按钮,在对话框。
I want to have an EditTextPreference that will disable the OK button if there is no text in the EditText field. I created a custom EditTextPreference class and I am able to get the EditText object and set a TextWatcher, but I can't find a way to disable the button. It looks like I just don't have access to the OK and Cancel buttons in the Dialog.
任何人都知道一个办法让这些按钮或做什么,我想干什么?
Anyone know a way to get at these buttons or to do what I am trying to do?
唯一的选择就是尽量从头创建一个自定义对话框,看起来像和模仿的EditText preference。
Only other option is to try to create from scratch a custom Dialog that looks like and mimics the EditTextPreference.
推荐答案
下面是一个code样品的启用/禁用按钮取决于 onCheckValue
函数返回真
或假
。
Here is a code sample that enables/disables button depending on whether onCheckValue
function returns true
or 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();
}
}
这篇关于的EditText preference禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!