我有一些使用Material Dialogs库的代码;我有一个包含四个EditText字段的对话框(电子邮件,名称,用户名,密码)。我希望它能够禁用对话框的注册按钮,除非所有字段中至少包含一个字符。
我四处搜寻,找到了办法做到这一点;不幸的是,它似乎不起作用。例如,如果我拉出对话框并在任何单个字段中键入任何内容,则该按钮将变为启用状态。但是,如果我编辑单个字段然后编辑另一个字段,然后删除其中一个字段中的文本 - 该按钮将被禁用;如果我要填写所有字段,然后删除单个字段中的文本,就会发生同样的事情。
我曾想过利用另一个建议(这将涉及写我自己的私人内部课程) - 但我并不认为考虑到我实现同样的事情会很重要(至少,据我所知。)
//registerDialog is a MaterialDialog object
final View registerAction = registerDialog.getActionButton(DialogAction.POSITIVE);
final EditText registerNameInput;
final EditText registerEmailInput;
final EditText registerUsernameInput;
final EditText registerPasswordInput;
if (registerDialog.getCustomView() != null) {
registerNameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_name);
registerEmailInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_email);
registerUsernameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_username);
registerPasswordInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_password);
/*
* TextWatcher lets us monitor the input fields while registering;
* This make sure we don't allow the user to register with empty fields
*/
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
registerAction.setEnabled(s.toString().trim().length() > 0);
}
@Override
public void afterTextChanged(Editable s) {
}
};
/*We want to watch all EditText fields for input*/
registerNameInput.addTextChangedListener(watcher);
registerEmailInput.addTextChangedListener(watcher);
registerUsernameInput.addTextChangedListener(watcher);
registerPasswordInput.addTextChangedListener(watcher);
}
registerDialog.show();
registerAction.setEnabled(false); //disabled by default
答案 0 :(得分:4)
将您的代码更改为:
首先,你需要将textwatcher添加到特定的EditText ,而你没有。请尝试以下方法:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
然后,
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (registerNameInput.getText().toString().length() > 0
&& registerEmailInput.getText().toString().length() > 0
&& registerUsernameInput.getText().toString().length() > 0
&& registerPasswordInput.getText().toString().length() > 0) {
registerAction.setEnabled(true);
} else {
registerAction.setEnabled(false);
}
}
答案 1 :(得分:0)
你可以这样做,但不要复制我没有检查代码。
public class MyTextWatcher implements TextWatcher {
EditText editText, anotherEditText;
View myView;
// Your constructor
public MyTextWatcher(EditText editText, EditText anotherEditText View myView) {
this.editText = editText;
this.myView = myView
this.anotherEditText = anotherEditText
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(anotherEditText.getText().toString().trim().lenght()>0){
myView.setEnabled(s.toString().trim().length() > 0);
}
}
@Override
public void afterTextChanged(Editable s) {//something }
使用此课程
editText.addTextChangedListener(new MyTextWatcher(editText, editText2, myView));
答案 2 :(得分:0)
对您的实施进行微小改动,您可以实现这一目标。您可以创建一个类似
的方法,而不是向onTextChanged添加逻辑private void validateInput(){
boolean enableButton = true;
if (registerNameInput.getText() == null || registerNameInput.getText().toString() == null || registerNameInput.getText().toString().trim().length() == 0){
enableButton = false;
}
if (registerEmailInput.getText() == null || registerEmailInput.getText().toString() == null || registerEmailInput.getText().toString().trim().length() == 0){
enableButton = false;
}
if (registerUsernameInput.getText() == null || registerUsernameInput.getText().toString() == null || registerUsernameInput.getText().toString().trim().length() == 0){
enableButton = false;
}
if (registerPasswordInput.getText() == null || registerPasswordInput.getText().toString() == null || registerPasswordInput.getText().toString().trim().length() == 0){
enableButton = false;
}
registerAction.setEnabled(enableButton);
}
并在afterTextChanged()
中调用此方法TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
validateInput();
}
};
逐个检查的好处是,例如,您要添加额外支票的电子邮件输入,然后您可以添加
if (registerEmailInput.getText() == null || registerEmailInput.getText().toString() == null || registerEmailInput.getText().toString().trim().length() == 0){
enableButton = false;
}else{
//put if block to check email input format is correct or not, if not then set enableButton = false;
}
答案 3 :(得分:0)
如果您想将单个TextWatcher类用于多个编辑文本,这只能在AfterTextChanged(可编辑的可编辑)之后才能使用
public void afterTextChanged(Editable editable) {
if (editable == editText1.getEditableText()) {
// do something
} else if (editable == editText2.getEditableText()) {
// do something
}
}