在我的应用程序的一个活动中,我有5个EditText字段。但是,我希望仅在其他4个EditTexts不为空时启用etPlayer5
。我写了这个小代码来实现这个目标:
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer3 = (EditText) findViewById(R.id.etPlayer3);
etPlayer4 = (EditText) findViewById(R.id.etPlayer4);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
if (namePlayer1.matches("")||namePlayer2.matches("")||namePlayer3.matches("")||namePlayer4.matches("")) {
etPlayer5.setEnabled(false);
etPlayer5.setFocusable(false);
}
else {
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
当然这段代码不能实时运行。当填写最后一个EditTexts 1到4时,我需要做什么,etPlayer5
是否自动设置为启用而不使用按钮或什么?
谢谢!
答案 0 :(得分:1)
你可以让textChangeListener包含所有4个Edittexts,并对所有edittext保持布尔值。当你调用onTextChange时,将booleans更新为true,并查看此edittext中的字符串长度是否为1,并进一步检查剩余的布尔也是如此。这就是诀窍。现在是时候编辑edittext了。希望有帮助。下面是上面解释的代码
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3, et4, et5;
boolean firsEditText, secondEditText, thirdEditText, fourthEditText, fifthEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
et1.addTextChangedListener(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) {
if (s.toString().length() > 0) {
firsEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
firsEditText = false;
et5.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et2.addTextChangedListener(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) {
if (s.toString().length() > 0) {
secondEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
secondEditText = false;
et5.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et3.addTextChangedListener(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) {
if (s.toString().length() > 0) {
thirdEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
thirdEditText = false;
et5.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et4.addTextChangedListener(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) {
if (s.toString().length() > 0) {
fourthEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
fourthEditText = false;
et5.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean checkAllTrue() {
if (firsEditText && secondEditText && thirdEditText && fourthEditText)
return true;
return false;
}
}
答案 1 :(得分:1)
你可以做这样的事情
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean player1Changed = false, player2Changed = false;
setContentView(R.layout.main);
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
etPlayer1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player1Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
etPlayer2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player2Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}