我制作了此代码以检查是否有任何编辑文本为空。我将它们全部放在一个数组中,然后检查每一个以查看它是否为空。如果有空的那个,就会出现一个祝酒词。如果全部都不为空,则应用继续。问题是,我的方法是排队所有编辑文本并冻结应用程序上的吐司。我该如何解决这个问题?
我的数组位于按钮onCreate:
final EditText[] editTextArray = new EditText[] {
(EditText) findViewById(R.id.num1),
(EditText) findViewById(R.id.num2),
(EditText) findViewById(R.id.num3),
(EditText) findViewById(R.id.num4),
(EditText) findViewById(R.id.num5),
(EditText) findViewById(R.id.num6),
(EditText) findViewById(R.id.num7),
(EditText) findViewById(R.id.num8),
(EditText) findViewById(R.id.num9),
(EditText) findViewById(R.id.num10),
(EditText) findViewById(R.id.num11),
(EditText) findViewById(R.id.num12),
(EditText) findViewById(R.id.num13),
(EditText) findViewById(R.id.num14),
(EditText) findViewById(R.id.num15),
(EditText) findViewById(R.id.num16),
};
循环:
for (EditText anEditTextArray : editTextArray) {
checkTextInput(anEditTextArray);
}
方法:
private boolean checkTextInput(EditText input) {
String inputNum = input.getText().toString();
if (inputNum.isEmpty()){
// Toast message
Toast.makeText(MainActivity.this, "The board is missing numbers.", Toast.LENGTH_SHORT).show();
return true;
} else {
Log.i(TAG, "working");
return false;
}
} // end checkTextInput
答案 0 :(得分:2)
您可以像这样修改for循环
for(EditText anEditTextArray : editTextArray) {
if(checkTextInput(anEditTextArray)) {
// an edit text is empty, so we need to stop the for loop to prevent lots of toast being shown on screen if there are more empty edit texts
break; // or use return based on the logic of your code
}
}
答案 1 :(得分:0)
在checkTextInput之外初始化Toast并在show之前取消它。所以它将停止前一个并显示新的。
Toast t = Toast.makeText(MainActivity.this, "The board is missing numbers.", Toast.LENGTH_SHORT);
private boolean checkTextInput(EditText input) {
String inputNum = input.getText().toString();
if (inputNum.isEmpty()){
// Toast message
t.cancel();
t.show();
return true;
} else {
Log.i(TAG, "working");
return false;
}
} // end checkTextInput