在使用两个不同的TextWatchers(一个用于名称,一个用于数字)验证六个EditTexts之后,我想启用我的“Fertig”按钮。因此我添加了一些变量[...]有效以检查是否满足所有条件。我是否必须更改if查询的位置?
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends Activity {
private TextView nachricht;
private Button weiterFertig;
private EditText vorname;
private EditText nachname;
private EditText strasse;
private EditText nummer;
private EditText plz;
private EditText stadt;
private boolean ersterKlick;
public boolean vnameValid;
public boolean nnameValid;
public boolean strasseValid;
public boolean stadtValid;
public boolean nummerValid;
public boolean plzValid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nachricht = (TextView) findViewById(R.id.nachricht);
weiterFertig = (Button) findViewById(R.id.weiter_Fertig);
vorname = (EditText) findViewById(R.id.vorname);
nachname = (EditText) findViewById(R.id.nachname);
strasse = (EditText) findViewById(R.id.strasse);
nummer = (EditText) findViewById(R.id.nummer);
plz = (EditText) findViewById(R.id.plz);
stadt = (EditText) findViewById(R.id.stadt);
ersterKlick = true;
vnameValid = false;
nnameValid = false;
strasseValid = false;
stadtValid = false;
nummerValid = false;
plzValid = false;
nachricht.setText(R.string.willkommen);
weiterFertig.setText(R.string.weiter);
weiterFertig.setEnabled(false);
weiterFertig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ersterKlick) {
nachricht.setText(getString(R.string.hallo, vorname.getText(), nachname.getText(), strasse.getText(), nummer.getText(), plz.getText(), stadt.getText()));
vorname.setVisibility(View.INVISIBLE);
nachname.setVisibility(View.INVISIBLE);
strasse.setVisibility(View.INVISIBLE);
nummer.setVisibility(View.INVISIBLE);
plz.setVisibility(View.INVISIBLE);
stadt.setVisibility(View.INVISIBLE);
weiterFertig.setText(R.string.fertig);
ersterKlick = false;
} else {
finish();
}
}
});
/*
* Validator1
*/
TextWatcher numberChecker = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence number, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence number, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable number) {
Pattern p = Pattern.compile("[0-9]{2,}");
Matcher m = p.matcher(number);
if (number.hashCode() == nummer.getText().hashCode()){
if (m.find()){
nummerValid = true;
} else{
nummerValid = false;
}
}
if (number.hashCode() == plz.getText().hashCode()){
if (m.find()){
plzValid = true;
} else{
plzValid = false;
}
}
}
};
/*
* Validator2
*/
TextWatcher inputChecker = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence input, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence input, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable input) {
Pattern p = Pattern.compile("^[a-zA-ZäöüÄÖÜß ]{3,}$");
Matcher m = p.matcher(input);
if (input.hashCode() == vorname.getText().hashCode()) {
if (m.find()) {
vnameValid = true;
} else {
vnameValid = false;
}
}
if (input.hashCode() == nachname.getText().hashCode()) {
if (m.find()) {
nnameValid = true;
} else {
nnameValid = false;
}
}
if (input.hashCode() == strasse.getText().hashCode()) {
if (m.find()) {
strasseValid = true;
} else {
strasseValid = false;
}
}
if (input.hashCode() == stadt.getText().hashCode()) {
if (m.find()) {
stadtValid = true;
} else {
stadtValid = false;
}
}
}
};
vorname.addTextChangedListener(inputChecker);
nachname.addTextChangedListener(inputChecker);
strasse.addTextChangedListener(inputChecker);
stadt.addTextChangedListener(inputChecker);
nummer.addTextChangedListener(numberChecker);
plz.addTextChangedListener(numberChecker);
if (vnameValid && nnameValid && strasseValid && stadtValid && nummerValid && plzValid) {
weiterFertig.setEnabled(true);
} else {
weiterFertig.setEnabled(false);
}
}
}
答案 0 :(得分:0)
您应该将该条件移动到另一个函数,并在两个验证器中调用它。当事件被触发时,他们会调用验证器并检查更改按钮的条件。
答案 1 :(得分:0)
您应该检查TextWatcher
中的启用或禁用按钮protected void onCreate(Bundle savedInstanceState) {
.....
TextWatcher numberChecker = new TextWatcher() {
Override
public void beforeTextChanged(CharSequence number, int start, int count, int after) {
}
@
Override
public void onTextChanged(CharSequence number, int start, int before, int count) {}
@
Override
public void afterTextChanged(Editable number) {
...
checkEnableOrDisable();
}
};
TextWatcher inputChecker = new TextWatcher() {
Override
public void beforeTextChanged(CharSequence number, int start, int count, int after) {
}
@
Override
public void onTextChanged(CharSequence number, int start, int before, int count) {}
@
Override
public void afterTextChanged(Editable number) {
...
checkEnableOrDisable();
}
};
.....
}
public void checkEnableOrDisable() {
if (vnameValid && nnameValid && strasseValid && stadtValid && nummerValid && plzValid) {
weiterFertig.setEnabled(true);
} else {
weiterFertig.setEnabled(false);
}
}
希望这个帮助