如何结束警报对话框?

时间:2012-08-03 10:36:06

标签: android

我正在应用程序中创建一个警告对话框,并希望在有人点击警告对话框中给出的特定按钮时结束它。即使dismiss()和cancel()对我不起作用。

这是警告对话框的代码:

    alertOnButtonDialog = new AlertDialog.Builder(this);
    TextView alertOnButtonView = new TextView(this);
    alertOnButtonView.setText("The website address of velosys consultancy services is \n velosysconsultancyservices.com.");

    Button alertOnButton = new Button(this);
    alertOnButton.setText("ok");
    alertOnButton.setOnClickListener(alertOnButtonClickListener);
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(1); 
    linearLayout.addView(alertOnButtonView);
    linearLayout.addView(alertOnButton);
    alertOnButtonDialog.setView(linearLayout);
    alertOnButtonDialog.show();

在这里,我想点击按钮结束它:

Button.OnClickListener alertOnButtonClickListener = new Button.OnClickListener()
    {  
        @Override  
        public void onClick(View v)
        {   
// code to end the alertDialog
        }
    };

请告诉我怎么做?

2 个答案:

答案 0 :(得分:1)

你做的是,只需使用builder。AlertDialog方法创建一个Create()的对象。

然后使用AlertDialog的dismiss()方法在按钮的单击中关闭该对话框。

AlertDialog alert;  
alertOnButtonDialog = new AlertDialog.Builder(this);
alert = alertOnButtonDialog.create();

现在,

Button.OnClickListener alertOnButtonClickListener = new Button.OnClickListener()
    {  
        @Override  
        public void onClick(View v)
        {   
         alert.dismiss();
        }
    };

答案 1 :(得分:1)

试试这个..

dismissDialog(YourDialog name);
相关问题