2016-11-28 133 views
0

我有一個活動,用4個EditTexts打開對話框,我需要用戶輸入所有編輯文本才能繼續,我如何強制用戶輸入所有字段?如何在按下按鈕後不關閉對話框

編輯 即使用戶按下後退按鈕 這是我的代碼:

private void showEnterNamesDialog(final boolean edit){ 
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    final View dialogView = inflater.inflate(R.layout.enter_names_dialog, null); 
    dialogBuilder.setView(dialogView); 

    final EditText[] plyrs_et = new EditText[4]; 

    plyrs_et[0] = (EditText)dialogView.findViewById(R.id.plyr1_et_dialog); 
    plyrs_et[1] = (EditText)dialogView.findViewById(R.id.plyr2_et_dialog); 
    plyrs_et[2] = (EditText)dialogView.findViewById(R.id.plyr3_et_dialog); 
    plyrs_et[3] = (EditText)dialogView.findViewById(R.id.plyr4_et_dialog); 

    if(edit){ 
     String[] names = gameList.getNames(); 
     for (int i = 0; i < 4; i++){ 
      plyrs_et[i].setText(names[i]); 
     } 

     dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 

      } 
     }); 
    } 

    dialogBuilder.setTitle(getResources().getString(R.string.enter_players_names)); 

    dialogBuilder.setPositiveButton(getResources().getString(R.string.submit_btn), new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 

      String[] names = new String[4]; 

      for(int j = 0; j < 4 ; j++){ 
       names[j] = plyrs_et[j].getText().toString(); 
      } 
      if(names[0].isEmpty() || names[1].isEmpty() || names[2].isEmpty() || names[3].isEmpty()) { 
       Toast error = Toast.makeText(getApplicationContext(), getResources().getString(R.string.toast_alert), Toast.LENGTH_SHORT); 
       error.show(); 
      }else { 
       if(edit){ 
        //set the names in textViews 
        hp1.setText(names[0]); 
        hp2.setText(names[1]); 
        hp3.setText(names[2]); 
        hp4.setText(names[3]); 

        gameList.setNames(names); 
       }else { 
        gameList.setNames(names); 
        setHeaderFooter(names); 
        listView.setAdapter(mAdapter); 
       } 
      } 
     } 
    }); 

    AlertDialog b = dialogBuilder.create(); 
    b.show(); 
} 

此代碼只給出了一個烤麪包的消息,但仍然關閉對話框。 任何想法?

+1

http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when - 按鈕被點擊 – Darussian

+0

我們需要覆蓋正面的按鈕...看到我的答案如下..我已經在我的項目 –

+0

做了同樣的感謝@Darussian我修復了它,但用戶仍然可以按後退按鈕 –

回答

0

通過覆蓋正面按鈕來嘗試此代碼。如果驗證失敗,它不會關閉警報對話框。

alertDialogBuilder.setCancelable(false) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // do nothing we overide this 
          } 
         }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 
           dialog.cancel(); 
          } 
         }); 
     alertDialogBuilder.setTitle("Committee Details"); 
     // create alert dialog 
     final AlertDialog alertDialog = alertDialogBuilder.create(); 
     // show it 
     alertDialog.show(); 
     alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(validate()) { 
        //your work after validation and close alert dialog 
        alertDialog.dismiss(); 
       } 
      } 
     }); 

和validate()方法可以強制驗證

public boolean validate() { 
    boolean t=true; 
    if(cName.getText().toString().length()==0) { 
     cName.setError("Commitee name cannot be Blank!!"); 
     t=false; 
    } 
    if(amount.length()==0){ 
     amount.setError("Amount cannot be Blank!!"); 
     t= false; 
    } 
    if(duration.getText().toString().length()==0) { 
     duration.setError("Duration cannot be Blank!!"); 
     t= false; 
    } 
    if(noOfMember.getText().toString().length()==0) { 
     noOfMember.setError("Number of Members cannot be Blank!!"); 
     t= false; 
    } 
    if(StartDate.length()<10) { 
     StartDate.setError("Input Date"); 
     t = false; 
    } 
    return t; 
} 
+0

@A蜂蜜B感謝編輯建議... –

相關問題