仅在填充时使用EditText关闭AlertDialog

时间:2014-07-16 04:25:43

标签: android android-edittext android-alertdialog dismiss

我在AlertDialog中有一个EditText。我想在EditText中有一些输入时忽略它,否则显示Toast提示用户输入数据。

目前,当编辑文本有数据时,对话框正确解除。此外,当edittext为空时,将显示toast。但是,即使EditText为空并且稍后显示Toast消息,对话框仍然会解除。

我不希望ALertDialog被解雇,并且在显示Toast后,EditText应该再次获得焦点。

这是我的代码: -

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();

1 个答案:

答案 0 :(得分:0)

您需要为您的要求编写自定义对话框。见Custom Dialog with Edit Text