如果输入编辑文本为空,我正在尝试禁用按钮。我正在使用文字观察者。为了进行测试,我只尝试了两个编辑文本。但是,无论如何我的按钮都可以启用。
@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)
}
从afterTextChanged(Editable s)
方法调用此方法
让我们仅在2个iDittexts上考虑这种情况。定义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中的每个文本内部分配值
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);
}
}
哦!你犯了一个小错误。使用OR
条件而不是AND
。因此,您的代码应为
nextBtn.setEnabled(!firstNameInput.isEmpty() || !lastNameInput.isEmpty());
和TextWatcher只能通知您何时手动更改EditText
的输入。因此,TextWatcher不会在开始时发挥作用。因此,首先在onCreate
方法中,您应该手动检查那些EditText
Feilds。
编辑:Android New DatabInding库最适合此目的。