使用EditText android解除AlertDialog

时间:2014-06-06 07:15:56

标签: android android-edittext alertdialog dismiss

我有一个AlertDialog,它有一个EditText。此EditText接受用户输入并在" Post"按钮单击API调用已启动。

问题是AlertDialog应该在" Post"单击按钮。目前我必须点击外面的区域来解雇它。

如果我删除API调用,则AlertDialog会在按钮单击时正确解除。

我不确定是什么问题。

这是我的代码: -

    case R.id.btnAddComms:
        scrollNews.fullScroll(v.FOCUS_DOWN);
        btnAddComms.setPressed(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(NDetails.this);
        builder.setTitle("Post Comment");
        builder.setIcon(R.drawable.post_comment_button);


        final EditText input1 = new EditText(NDetails.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        input1.setLayoutParams(lp);
        builder.setView(input1);


        builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
        {
            //private ITextInputDialogCalback callback;

            public void onClick(DialogInterface dialog, int id) 
            {



                 postedComment = input1.getText().toString();
                 if(postedComment.length()>0)
                 {
                     dialog.cancel();
                     PostComments(postedComment);

                 }
                 else
                 {
                     Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                     input1.findFocus();
                 }





            }

            private void PostComments(String postedComment)
            {
                // TODO Auto-generated method stub

                  String postCommentUrl  = url;
                    try 
                    {
                        String commentResponse = new PostComment().execute(postCommentUrl).get();
                        String getRequestForComments = myurl;
                        String items = new FetchItems().execute(getRequestForComments).get();
                        ArrayList<HashMap<String, String>> updatedCommentList = new GetList().execute(items).get();



                        itemsAdapter = (ListAdapter) new CommentsAdapter(NDetails.this, updatedCommentList);
                        commentsList.invalidate();
                        commentsList.refreshDrawableState();
                        commentsList.setAdapter(itemsAdapter);



                        commentsList.post(new Runnable() 
                        {

                            @Override
                            public void run() 
                            {
                                // TODO Auto-generated method stub
                                commentsList.setSelection(itemsAdapter.getCount()-1);

                            }
                        });


                    } 
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    catch (ExecutionException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }
        })
        .setCancelable(false);



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

        alert.show();

        break;

4 个答案:

答案 0 :(得分:1)

而不是

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

    alert.show();

    break;

使用

    builder.create().show();

    break;

答案 1 :(得分:0)

做下一个:

AlertDialog alert; // Define it in your activity or fragment

... 

case R.id.btnAddComms:

            ...

            builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
            {
                //private ITextInputDialogCalback callback;

                public void onClick(DialogInterface dialog, int id) 
                {

                     postedComment = input1.getText().toString();
                     if(postedComment.length()>0)
                     {
                         alert.dismiss(); // Dismiss dialog here
                         PostComments(postedComment);

                     }
                     else
                     {
                         Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                         input1.findFocus();
                     }

          ...


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

          alert.show();

          break;

答案 2 :(得分:0)

只需更换对话框&#34; post&#34;按钮点击代码到下面的代码:

<强>更新:

AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setMessage("your message");     

        builder.setPositiveButton("Post", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                 postedComment = input1.getText().toString();
                     if(postedComment.length()>0)
                     {

                         PostComments(postedComment);
                         dialog.dismiss();
                     }
                     else
                     {
                         Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
                         input1.findFocus();
                     }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
        {   
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                dialog.dismiss();
            }
        });

        builder.show();

答案 3 :(得分:0)

尝试以不同的方式创建对话框。可以使用这样的东西:

new AlertDialog.Builder(ACT_NewsListings.this)
            .setTitle("Title")
            .setMessage("Message")
            .setCancelable(false)

            .setPositiveButton("Post", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    dialog.dismiss();
                                        //YOUR STUFF HERE
                }
            })
            .show();