2014-07-16 51 views
0

我在AlertDialog中有一個EditText。我只想在EditText中有一些輸入時解除它,否則會顯示Toast提示用戶輸入數據。僅在填充EditText時才關閉AlertDialog

當前,當編輯文本有數據時,對話框會被正確解除。另外,當edittext爲空時,會顯示toast。然而,即使EditText爲空並且Toast消息稍後顯示,對話框仍然會消失。

我不希望ALertDialog關閉,EditText應該在顯示Toast後再次獲得焦點。

這裏是我的代碼: -

builder.setPositiveButton("Post", new DialogInterface.OnClickListener() 
      { 

       public void onClick(DialogInterface dialog, int id) 
       { 

        boolean worked = true; 
        postedComment = input1.getText().toString(); 

        if(postedComment.length()==0 || postedComment=="" || postedComment.matches("")) 
        { 

         Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show(); 
         input1.findFocus(); 
         worked = false; 
        } 
        else if(worked && postedComment!="") 
        { 

         dialog.dismiss(); 
         pd = new ProgressDialog(NewsDetails.this); 
         pd.setMessage("Posting.."); 
         pd.show(); 
         pd.setCancelable(true); 

         PostComments(postedComment); 
        } 
       }) 
.setCancelable(false); 

      alert = builder.create(); 
      alert.setCanceledOnTouchOutside(true); 

      alert.show(); 
+4

您需要使用自定義對話框而非alert.Because在按鈕單擊後沒有控制權。這是按鈕點擊後,它會默認關閉。 –

+0

@Biraj你能指點我的一個例子嗎? – user3146095

+0

另外,你應該使用'equals()'方法來比較字符串,而不是像這樣:'postedComment ==「」'。在你的情況下''TextUtils.isEmpty()'方法也是有用的。 –

回答