2014-07-16 49 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()'方法也是有用的。 –

回答