我无法关闭警报对话框。我正在使用布局充气器进行对话,所以我不确定在完成它之后我会怎样关闭它。代码如下:
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();
我无法调用finish,因为它关闭了启动警报对话框的列表视图,并且对我无法使用dialog.dismiss调用。
您认为我应该怎么做才能解决这个问题?
答案 0 :(得分:32)
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();
答案 1 :(得分:16)
这样做,
add_item.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
});
答案 2 :(得分:12)
试试这个:
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对象。这适合我。让我知道它是怎么回事。
答案 3 :(得分:3)
我修改了你的代码plz check ..
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(布局); 强> 至 的 Dial.setView(布局); 强>
现在只需在onclick监听器中调用 Dial.dismiss(); 即可。 对我来说很好。
答案 4 :(得分:1)
在您的班级中使用此代码:
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,然后将此代码粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Ok "/>
</RelativeLayout>
来源:https://www.mkyong.com/android/android-custom-dialog-example/
如果您不喜欢上面的代码,请参阅以下链接: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/
答案 5 :(得分:0)
尝试dialog.cancel();在你的onclick听众。 它应该工作。
答案 6 :(得分:0)
您需要实现DialogInterface.OnClickListener而不是View.OnClickListener。然后dialog.dismiss()将可用。
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
}
答案 7 :(得分:0)
您遇到错误,请尝试使用此代码
dialog.setView(layout);
final AlertDialog alertDialog = dialog.create();
add_item.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if (alertDialog != null && alertDialog.isShowing()) {
alertDialog.dismiss();
}
}
});
alertDialog.show();
答案 8 :(得分:0)
final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(DebugActivity.this);
viewProgressDialogue.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("response","im calling");
viewProgressDialogue.dismiss();
}
}, 5000);
> Make it one Instance so that it will work It Will in the Fragment too. for Fragment Use
final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(getActivity());
viewProgressDialogue.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("response", "im calling");
viewProgressDialogue.dismiss();
}
}, 5000);
> viewProgressDialogue Class
public class viewProgressDialogue extends Dialog {
public viewProgressDialogue(@NonNull Context context) {
super(context);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
View view = LayoutInflater.from(context).inflate(
R.layout.custom_progress, null);
setContentView(view);
}
}
答案 9 :(得分:-1)
尝试一下:
<a href="$1">$2</a>