如何为 class 之外的自定义对话框设置 onDismiss/CancelListener?
How to set onDismiss/CancelListener for custom dialog outside the class?
我想在取消或关闭自定义对话框时完成 activity。但是当我在其他 classes 中使用 .setOnDismissListener 时,它永远不会在内部到达。我发现了几个问题,但解决方案是覆盖 customDialog class 中的 onDismiss 方法。但是我不需要为我创建的每个 customDialog 覆盖 onDismiss 方法。我该怎么办?
这是我在另一个 class 中调用的代码,但从未在日志 "setOnDismissListener" 中收到消息。
customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.d(TAG, "setOnDismissListener");
}
});
我的自定义对话框class:
public class CustomDialog extends Dialog {
private static final String TAG = "CustomDialog";
public CustomDialog(Context context, String title, String message) {
super(context);
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setPadding(10, 50, 10, 10);
textView.setText(title);
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
textView.setTypeface(boldTypeface);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder
.setCustomTitle(textView)
.setMessage(message)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog customDialog = builder.show();
TextView messageText = customDialog.findViewById(android.R.id.message);
if (messageText != null) {
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.GRAY);
} else {
Log.w(TAG, "messageText is null");
}
}
}
做一个像
这样的界面
interface OnUserInformedCallback {
fun positive()
fun negative()
}
并在你的 activity 中实现它,并将它传递给你正在获取对话框的对话框方法,你将在 activity 中获得 ok 和 cancle 的回调。
代码就是这样
fun alertDialog(
context: Context,
message: String,
positiveText: String,
isUa: Boolean,
callback: OnUserInformedCallback
): Dialog {
val dialog = Dialog(context, android.R.style.Theme_Translucent_NoTitleBar)
dialog.setContentView(R.layout.alert_dialog_layout)
dialog.window?.setGravity(Gravity.CENTER)
dialog.setCancelable(true)
dialog.setCanceledOnTouchOutside(true)
val tvOk: TextView = dialog.findViewById(R.id.visit)
val tvMsg: TextView = dialog.findViewById(R.id.mess)
tvMsg.text = message
tvOk.text = positiveText
dialog.findViewById<TextView>(R.id.cancel).setOnClickListener {
callback.negative()
}
tvOk.setOnClickListener {
callback.positive()
}
dialog.create()
return dialog
}
在 java 中用于默认对话框
private AlertDialog getDialog(Activity context, String message, OnUserInformedCallback callbac) {
return new AlertDialog.Builder(context)
.setTitle(R.string.app_name).setMessage(message)
.setCancelable(true)
.setPositiveButton(android.R.string.yes, (dialog12, which) -> callbac.positive())
.setNegativeButton(android.R.string.yes, (dialog1, which) -> callbac.positive())
.create();
}
是的,所以如果您没有使用某些 API 来解析信息,或者正在使用局部变量,我建议您在 onClickListener() 方法中执行任何您想执行的功能。
The problem is that you are using your CustomDialog which itself extends the Dialog Class. But instead of using that you create a new alert dialog and build that. You dismiss it, but the dialog which is dismissed is not your custom dialog class, but the builder dialog you created in your constructor. Even if you fixed for that, it introduces unnecessary complications.
我建议您在 onClickListener() 函数中创建 Intent。这样做的方法是更改您的构造函数以支持回调侦听器。简而言之,当您收听的对话是另一个对话时,您不能只添加一个 onDismissListener() 。您可以做的是在用户将对话框作为特殊情况关闭时传入您想要执行的功能。见下文。
所以,首先,像这样修改你的构造函数:
public CustomDialog(Context context, String title, String message,
DialogInterface.OnClickListener listener) {
super(context);
}
在您的构造函数中粘贴您之前的代码:
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setPadding(10, 50, 10, 10);
textView.setText(title);
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
textView.setTypeface(boldTypeface);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder
.setCustomTitle(textView)
.setMessage(message)
.setPositiveButton("Ok", listener);
AlertDialog customDialog = builder.show();
TextView messageText = customDialog.findViewById(android.R.id.message);
if (messageText != null) {
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.GRAY);
} else {
Log.w(TAG, "messageText is null");
}
你所做的就是你以前创建一个新的 onClickListener() 你传入监听器参数的地方。
转到您的 MainActivity 或您创建自定义对话框的位置。这样做:
CustomDialog customDialog = new CustomDialog(FirstActivity.this, "Title", "Message",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do your functionality here.
Intent intent = new Intent(context, activity.class);
//Add any flags if you want
...
context.startActivity(intent);
//Or you can simply do context.finish();
}
});
当您不想传递 onClickListener() 时(意思是当您不想 finish()
activity 时)传入 null
.
它会起作用的。如果这不是您想要的,请告诉我,我会修复它。
我想在取消或关闭自定义对话框时完成 activity。但是当我在其他 classes 中使用 .setOnDismissListener 时,它永远不会在内部到达。我发现了几个问题,但解决方案是覆盖 customDialog class 中的 onDismiss 方法。但是我不需要为我创建的每个 customDialog 覆盖 onDismiss 方法。我该怎么办?
这是我在另一个 class 中调用的代码,但从未在日志 "setOnDismissListener" 中收到消息。
customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.d(TAG, "setOnDismissListener");
}
});
我的自定义对话框class:
public class CustomDialog extends Dialog {
private static final String TAG = "CustomDialog";
public CustomDialog(Context context, String title, String message) {
super(context);
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setPadding(10, 50, 10, 10);
textView.setText(title);
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
textView.setTypeface(boldTypeface);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder
.setCustomTitle(textView)
.setMessage(message)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog customDialog = builder.show();
TextView messageText = customDialog.findViewById(android.R.id.message);
if (messageText != null) {
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.GRAY);
} else {
Log.w(TAG, "messageText is null");
}
}
}
做一个像
这样的界面interface OnUserInformedCallback {
fun positive()
fun negative()
}
并在你的 activity 中实现它,并将它传递给你正在获取对话框的对话框方法,你将在 activity 中获得 ok 和 cancle 的回调。 代码就是这样
fun alertDialog(
context: Context,
message: String,
positiveText: String,
isUa: Boolean,
callback: OnUserInformedCallback
): Dialog {
val dialog = Dialog(context, android.R.style.Theme_Translucent_NoTitleBar)
dialog.setContentView(R.layout.alert_dialog_layout)
dialog.window?.setGravity(Gravity.CENTER)
dialog.setCancelable(true)
dialog.setCanceledOnTouchOutside(true)
val tvOk: TextView = dialog.findViewById(R.id.visit)
val tvMsg: TextView = dialog.findViewById(R.id.mess)
tvMsg.text = message
tvOk.text = positiveText
dialog.findViewById<TextView>(R.id.cancel).setOnClickListener {
callback.negative()
}
tvOk.setOnClickListener {
callback.positive()
}
dialog.create()
return dialog
}
在 java 中用于默认对话框
private AlertDialog getDialog(Activity context, String message, OnUserInformedCallback callbac) {
return new AlertDialog.Builder(context)
.setTitle(R.string.app_name).setMessage(message)
.setCancelable(true)
.setPositiveButton(android.R.string.yes, (dialog12, which) -> callbac.positive())
.setNegativeButton(android.R.string.yes, (dialog1, which) -> callbac.positive())
.create();
}
是的,所以如果您没有使用某些 API 来解析信息,或者正在使用局部变量,我建议您在 onClickListener() 方法中执行任何您想执行的功能。
The problem is that you are using your CustomDialog which itself extends the Dialog Class. But instead of using that you create a new alert dialog and build that. You dismiss it, but the dialog which is dismissed is not your custom dialog class, but the builder dialog you created in your constructor. Even if you fixed for that, it introduces unnecessary complications.
我建议您在 onClickListener() 函数中创建 Intent。这样做的方法是更改您的构造函数以支持回调侦听器。简而言之,当您收听的对话是另一个对话时,您不能只添加一个 onDismissListener() 。您可以做的是在用户将对话框作为特殊情况关闭时传入您想要执行的功能。见下文。
所以,首先,像这样修改你的构造函数:
public CustomDialog(Context context, String title, String message, DialogInterface.OnClickListener listener) { super(context); }
在您的构造函数中粘贴您之前的代码:
TextView textView = new TextView(context); textView.setGravity(Gravity.CENTER); textView.setPadding(10, 50, 10, 10); textView.setText(title); textView.setTextColor(Color.BLACK); textView.setTextSize(20); Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); textView.setTypeface(boldTypeface); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder .setCustomTitle(textView) .setMessage(message) .setPositiveButton("Ok", listener); AlertDialog customDialog = builder.show(); TextView messageText = customDialog.findViewById(android.R.id.message); if (messageText != null) { messageText.setGravity(Gravity.CENTER); messageText.setTextColor(Color.GRAY); } else { Log.w(TAG, "messageText is null"); }
你所做的就是你以前创建一个新的 onClickListener() 你传入监听器参数的地方。
转到您的 MainActivity 或您创建自定义对话框的位置。这样做:
CustomDialog customDialog = new CustomDialog(FirstActivity.this, "Title", "Message", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Do your functionality here. Intent intent = new Intent(context, activity.class); //Add any flags if you want ... context.startActivity(intent); //Or you can simply do context.finish(); } });
当您不想传递 onClickListener() 时(意思是当您不想
finish()
activity 时)传入null
.
它会起作用的。如果这不是您想要的,请告诉我,我会修复它。