开发者

How to Cancel a AlertDialog.Builder

What Should i Write in the Runnable Run Method to cancel the Alert.Bulider??

AlertDialog.Builder ad;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Con开发者_开发问答text context=this;
     ad = new AlertDialog.Builder(context);
        ad.setTitle("Warning");
        ad.setMessage("Just Testing It");

        ad.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        ad.setNegativeButton("Nooooo", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });

        ad.show();
        Handler h=new Handler();
        h.postAtTime(r, 10000);

    }
    public Runnable r=new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }
    };


You cannot hide a AlertDialog.Builder. Instead declare the member variable ad as AlertDialog, create the AlertDialog using the builder and assign it to ad by writing ad = builder.create(). In the run method call ad.cancel();


show() returns AlertDialog so make a variable as below:

AlertDialog dlg = ad.show();

and then dismiss when required -

dlg.dismiss();


I just set null for listener, it works for me.

ad.setNagativeButton(yourMessage, null);

Hope it helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜