2017-05-12 62 views
-1

我使用AlertDialog構建器從用戶接收一些輸入,在那裏我寫了一個小的驗證,如:如果EditText爲空顯示SnackBar並且不關閉對話。如果EditText爲空,請不要關閉警報對話框 - Android AlertDialog.Builder

發生了什麼,一旦用戶將EditText留空並點擊Positive按鈕,即可在Snackbar中獲取消息,還可以關閉對話框。

所以,我怎麼能控制在AlertDialog關閉,如果條件失敗,這裏是代碼:

public void inputDialog() { 

     LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context); 
     final View mView = layoutInflaterAndroid.inflate(R.layout.layout_dialog, null); 
     final AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context); 

     acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput); 

     alertDialogBuilderUserInput.setView(mView); 

     alertDialogBuilderUserInput 
       .setCancelable(false) 
       .setPositiveButton("SAVE", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialogBox, int id) { 
         // ToDo get user input here 

         String strUserInput = acceptUserInput.getText().toString().trim(); 
         if(TextUtils.isEmpty(strUserInput)) { 

          Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG); 
          snackbar.show(); 

          return; 

         } 
        } 
       }) 

       .setNegativeButton("CLOSE", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialogBox, int id) { 
           dialogBox.cancel(); 
          } 
         }); 

     AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create(); 
     alertDialogAndroid.show(); 

     } 
+0

r回報;從正面的按鈕 –

+2

試試這個:使用onShow()method.http://stackoverflow.com/a/7636468/3960700 – Steve

+0

@Sophie嘗試上傳我的答案。它完美的作品。 –

回答

0

您可以設置onShowListener如下:

View view = getLayoutInflater().inflate(R.layout.layout_dialog, null); 

acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput); 

    final AlertDialog alertDialog = new AlertDialog.Builder(context) 
      .setView(view) 
      .setCancelable(false) 
      .setPositiveButton("SAVE", null) 
      .setNegativeButton("CLOSE", null) 
      .create(); 

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 

      Button buttonPositive = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); 
      buttonPositive.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View view) { 
       String strUserInput = acceptUserInput.getText().toString().trim(); 
         if(TextUtils.isEmpty(strUserInput)) { 

          Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG); 
          snackbar.show(); 
          return; 
         } 
      }); 

      Button buttonNegative = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
      buttonNegative.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View view) { 
        alertDialog.dismiss(); 
       } 
      }); 
     } 
    }); 
0
AlertDialog.Builder mDialog = new AlertDialog.Builder(
      MapActivity.this);  

AlertDialog mAlertDialog = mDialog.create(); 
      mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 

       @Override 
       public void onShow(DialogInterface dialog) { 

        Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
        b.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View view) { 
          // TODO Do something 

          mEdt.setText("message"); 
         } 
        }); 
       } 
      }); 
      mAlertDialog.show(); 
0

工作解決方案您的問題如下:

public void inputDialog() { 

    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context); 
    final View mView = layoutInflaterAndroid.inflate(R.layout.layout_dialog, null); 
    final AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context); 

    final EditText acceptUserInput = (EditText) mView.findViewById(R.id.acceptUserInput); 

    alertDialogBuilderUserInput.setView(mView); 

    alertDialogBuilderUserInput 
      .setCancelable(false) 
      .setPositiveButton("SAVE", null) 
      .setNegativeButton("CLOSE",null); 

    final AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create(); 
    alertDialogAndroid.show(); 
    alertDialogAndroid.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      String strUserInput = acceptUserInput.getText().toString().trim(); 
      if(TextUtils.isEmpty(strUserInput)) { 

       Snackbar snackbar = Snackbar.make(mView, "Name field cannot be left blank", Snackbar.LENGTH_LONG); 
       snackbar.show(); 
      } 
     } 
    }); 
    alertDialogAndroid.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      alertDialogAndroid.cancel(); 
     } 
    }); 
}