单击按钮时关闭自定义警报对话框 [英] Closing a custom alert dialog on button click

查看:35
本文介绍了单击按钮时关闭自定义警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法关闭警报对话框.我正在使用布局充气器来制作对话框,所以我不确定在完成它后我将如何关闭它.代码如下:

I'm having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I'm not sure how I would go about closing the thing after I'm done with it. Code is below:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

add_item.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
        //Need this to close the alert dialog box
        }
});

title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();

我无法调用完成,因为这会关闭启动警报对话框的列表视图,并且我无法使用 dialog.dismiss 调用.

I cant call finish, because that closes the listview that the alert dialog is launched from, and the dialog.dismiss calls are not available to me.

你认为我应该怎么做才能解决这个问题?

What do you think I should do to fix this?

推荐答案

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

title.setText(workout_items[position]);
dialog.setView(layout);

AlertDialog alertDialog = dialog.create();

add_item.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        alertDialog.dismiss();
    }
});


alertDialog.show();

这篇关于单击按钮时关闭自定义警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆