如果我的输入编辑文本为空,我试图禁用我的按钮。我为此使用文本监视程序。为了进行测试,我仅尝试了两个编辑文本来开始。 但是,无论如何,我的按钮均保持启用状态。
@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) {
}
};
我希望如果一个或所有输入为空,则该按钮将被禁用,并且在填写所有输入字段时,该按钮将被启用。
答案 0 :(得分:0)
创建一个方法,检查那里的所有条件
layout.swig
从private void checkTheConditions(){
if(condition1 && condition2)
nextBtn.setEnabled(true)
else
nextBtn.setEnabled(false)
}
方法中调用此方法
答案 1 :(得分:0)
让我们现在仅考虑2种EditText的这种情况。 如下定义2个全局CharSequence
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);
}
}
答案 2 :(得分:-1)
哦!您犯了一个小错误。使用OR
条件而不是AND
。所以你的代码应该是
nextBtn.setEnabled(!firstNameInput.isEmpty() || !lastNameInput.isEmpty());
并且TextWatcher仅在您手动更改EditText
的输入时通知。因此,TextWatcher不会在启动时出现扭曲。因此,首先使用onCreate
方法,您应该手动检查那些EditText
领域。
编辑: Android新的DataBinding库最适合此目的。