一次,我在考慮的EditText來自用戶的輸入。現在我想在另一個文本框中顯示所需的輸出,但是如果用戶輸入了錯誤的值,則應該打開一個對話框,提及所有不正確的值...... box會一直打開,直到它檢測到所有不正確的值。例如,如果我在編輯框中添加三個錯誤的值,它會打開3次。
String s=editText1.getText().toString();
String z[]=s.split("\\s");
editText2.setText("");
String a = "";
String b = " Not valid";
for(int i=0;i<z.length;i++)
{
int j=Integer.parseInt(z[i]);
if(j>=65 && j<=97)
{
editText2.setText(editText2.getText() + "" + String.valueOf((char) j));
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
a += z[i]+"\t";
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(a+b)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
因爲你alertDialog.show();在for循環 – user3040153
保持foor循環的外側 準備您的消息在其他&呼叫alertDialog.show();循環結束後 – user3040153
@ user3040153如果我在for循環之外並且調用alertDialogue.show(),即使用戶輸入正確的值,它仍然打開一次 – Rohan