Java 如何使用Textwatcher禁用具有多个编辑文本的按钮?
如果输入的编辑文本为空,我将尝试禁用按钮。我正在使用文本监视程序。为了测试它,我只尝试了两个编辑文本开始。 但是,无论发生什么情况,我的按钮都保持启用状态Java 如何使用Textwatcher禁用具有多个编辑文本的按钮?,java,android,android-edittext,textwatcher,Java,Android,Android Edittext,Textwatcher,如果输入的编辑文本为空,我将尝试禁用按钮。我正在使用文本监视程序。为了测试它,我只尝试了两个编辑文本开始。 但是,无论发生什么情况,我的按钮都保持启用状态 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_profile); fnameInput
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
fnameInput = findViewById(R.id.et_firstname);
lnameInput = findViewById(R.id.et_lastname);
numberInput = findViewById(R.id.et_phone);
emailInput = findViewById(R.id.et_email);
nextBtn = findViewById(R.id.btn_next);
fnameInput.addTextChangedListener(loginTextWatcher);
lnameInput.addTextChangedListener(loginTextWatcher);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchNextActivity();
}
});
}
文本观察方法
private TextWatcher loginTextWatcher = 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) {
String firstNameInput =firstNameInput.getText().toString().trim();
String lastNameInput = lastNameInput.getText().toString().trim();
// tried doing it this way
nextBtn.setEnabled(!firstNameInput.isEmpty() && !lastNameInput.isEmpty());
//What i've also tried
if(firstNameInput.length()> 0 &&
lastNameInput.length()>0){
nextBtn.setEnabled(true);
} else{
nextBtn.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
如果一个或所有输入为空,我希望该按钮被禁用,并且在填写所有输入字段时启用。创建一个方法检查所有条件,如
private void checkTheConditions(){
if(condition1 && condition2)
nextBtn.setEnabled(true)
else
nextBtn.setEnabled(false)
}
从
后文本更改(可编辑的s)
方法调用此方法哦!你犯了一个小错误。使用或
条件代替和
。所以你的代码应该是
nextBtn.setEnabled(!firstNameInput.isEmpty() || !lastNameInput.isEmpty());
只有当您手动更改EditText
的输入时,TextWatcher才会发出通知。所以TextWatcher在启动时不会发抖。因此,首先在onCreate
方法中,您应该手动检查那些EditText
feild
编辑:
Android新的数据绑定库最适合这个目的。
让我们只考虑现在的2个编辑文本。 定义2个全局字符序列,如下所示
CharSequence fName="";
CharSequence lName="";
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
fnameInput = findViewById(R.id.et_firstname);
lnameInput = findViewById(R.id.et_lastname);
numberInput = findViewById(R.id.et_phone);
emailInput = findViewById(R.id.et_email);
nextBtn = findViewById(R.id.btn_next);
fnameInput.addTextChangedListener(textWatcher);
lnameInput.addTextChangedListener(textWatcher2);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchNextActivity();
}
});
}
然后,您必须为每个Edittext定义不同的textwatcher
然后,在这些textWatcher中,为上面定义的CharSequence分配值
private TextWatcher textWatcher = 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) {
fName=s;
validate(); //method to enable or disable button (find method below)
}
@Override
public void afterTextChanged(Editable s) {
}
};
现在是textWatcher2
private TextWatcher textWatcher2 = 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) {
lName=s;
validate(); //method to enable or disable button (find method below)
}
@Override
public void afterTextChanged(Editable s) {
}
};
现在编写验证方法
void validate(){
if (fName.length()>0 && lName.length()>0){
nextBtn.setEnabled(true);
}else {
nextBtn.setEnabled(false);
}
}
我将button.setEnabled(false)设置为on,onCreate使我的按钮在第一次查看时被禁用,并更改了一些逻辑。但是除此之外,当placed-afterTextChanged时,这个条件起作用了。好的,我写这个答案时考虑了一些事情,比如验证用户名和检查密码强度。如果您有多个textWatcher,并且您正在检查不同的条件,那么这个答案将帮助您检查Android数据绑定库和LiveData。您不需要编写这些示例代码。