我无法关闭警报对话框。 我正在使用布局充气器来进行对话,所以我不确定在完成它之后我将如何关闭它。 代码如下:
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(); } }