0

我有一个AlertDialog.Builder里面有一个按钮,我想在点击它时关闭对话框。

但是没有.dismiss()or.cancel()方法。

    LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inf.inflate(R.layout.map_window, null);

    window = new AlertDialog.Builder(activity);
    window.setCancelable(true);

    buttonStar = (ImageButton)layout.findViewById(R.id.buttonStar);
    buttonStar.setBackgroundResource(R.drawable.star);
    buttonStar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        //finishing window;
    }
      });


    window.setPositiveButton("Volver", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int wichButton) {  

        }  
    });  

    window.show();

}
4

3 回答 3

4

AlertDialog.Builder#show返回AlertDialog自身,因此只需获取从其返回的 AlertDialog 本身show并对其调用dismiss:

AlertDialog dialog;
//...

dialog = window.show();
于 2012-05-16T09:56:49.353 回答
2

尝试从对话框对象中取消它。创建一个对话框对象如下:

Dialog dialog = window.create();
dialog.cancel()
于 2012-05-16T09:58:43.227 回答
0

找到下面的 AlertDialog 代码。使用新的 DialogInterface.OnClickListener() 而不是 onClick()。

    AlertDialog ad = new AlertDialog.Builder(MainCatalogueActivity.this)

                    .setTitle("Server Response Error")

                    .setMessage("some message ")

                    .setIcon(R.drawable.ic_menu_close_clear_cancel)

                    .setNegativeButton("Close", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Intent intent = new Intent(Intent.ACTION_MAIN);

                            intent.addCategory(Intent.CATEGORY_HOME);

                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                            startActivity(intent);

                            finish();


                        }
                    }).create();
    ad.show();
于 2012-05-16T10:18:21.050 回答