2017-02-18 43 views
0

enter image description here如何顯示在對話框中只有不正確的值僅

enter image description here

一次,我在考慮的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(); 
     } 
+0

因爲你alertDialog.show();在for循環 – user3040153

+0

保持foor循環的外側 準備您的消息在其他&呼叫alertDialog.show();循環結束後 – user3040153

+0

@ user3040153如果我在for循環之外並且調用alertDialogue.show(),即使用戶輸入正確的值,它仍然打開一次 – Rohan

回答

1
boolean is_open_dialog=false; 

    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 { 

      is_open_dialog = true; 
      a += z[i]+"\t"; 

     } 
    } 

    if(is_open_dialog){ 


     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     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(); 

    } 
+0

我認爲這可以幫助你@Rohan –

+0

爲什麼布爾突出顯示紅色..如何更正此錯誤 – Rohan

+0

對不起使用布爾值。拼寫錯誤。 –