android自定义按钮弹出窗关闭按钮,在按钮单击时关闭自定义警报对话框

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

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调用。

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

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();

像这样做,

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

尝试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null); builder.setView(dialogView); closeBtn = (Button)dialogView.findViewById(R.id.close_btn); final AlertDialog dialog = builder.create(); closeBtn .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show();

您应该使用AlertDialog.Builder来“构建”警报对话框本身。 然后,在AlertDialog.Builder对象上调用create()方法。 此方法返回一个AlertDialog对象,允许您调用dismiss()。

您不必多次创建它,也不必使用任何DialogInterface对象。 这对我有用。 让我知道它是怎么回事。

我修改了你的代码plz检查..

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); final AlertDialog Dial = alertDialog.create(); title.setText(workout_items[position]); Dial.setView(layout); AlertDialog alertDialog = dialog.create(); add_item.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Dial.dismiss(); } }); Dial.show();

刚刚添加

final AlertDialog Dial = alertDialog.create(); 并更改dialog.setView(layout); 到Dial.setView(布局);

现在只需调用Dial.dismiss(); 在onclick听众..对我来说很好。

在您的class级中使用此代码:

Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom); ((Button) dialog.findViewById(R.id.button)) .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show();

并创建custom.xml,然后将此代码粘贴到其中:

来源: https : //www.mkyong.com/android/android-custom-dialog-example/

如果您不喜欢上面的代码,请参阅以下链接: http : //thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/

尝试dialog.cancel(); 在你的onclick听众。 它应该工作。

您需要实现DialogInterface.OnClickListener而不是View.OnClickListener。 那么dialog.dismiss()将可用。

new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ dialog.dismiss(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值