如何取消AlertDialog.Builder

时间:2011-04-05 04:55:54

标签: android

我应该在Runnable Run方法中写什么来取消Alert.Builder?

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

        Context 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

        }
    };

2 个答案:

答案 0 :(得分:23)

您无法隐藏AlertDialog.Builder。而是将成员变量ad声明为AlertDialog,使用构建器创建AlertDialog,并通过编写ad = builder.create()将其分配给广告。在run方法中调用ad.cancel();

答案 1 :(得分:19)

show()返回AlertDialog,因此请按以下方式输入变量:

AlertDialog dlg = ad.show();

然后在需要时解雇 -

dlg.dismiss();