如何为类外的自定义对话框设置onDismiss/CancelListener?

我想在取消或取消自定义对话框时完成活动。但是当我在其他类中使用 .setOnDismissListener 时,它永远不会在内部达到。我发现了几个问题,但解决方案是在 customDialog 类中覆盖 onDismiss 方法。但是我不需要为我创建的每个 customDialog 覆盖 onDismiss 方法。我应该怎么办?


这是我在另一个类中调用的代码,但从未在日志“setOnDismissListener”中收到消息。


customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

    @Override

    public void onDismiss(DialogInterface dialog) {                                                       

        Log.d(TAG, "setOnDismissListener");

    }

});

我的 CustomDialog 类:


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

        }

    }

}


紫衣仙女
浏览 98回答 2
2回答

繁星淼淼

做一个像这样的界面interface OnUserInformedCallback {&nbsp; &nbsp; fun positive()&nbsp; &nbsp; fun negative()}并在您的活动中实现它并将其传递给您正在获取对话框的对话框方法,您将在您的活动中获得 ok 和 cancle 的回调。代码就是这样&nbsp;fun alertDialog(&nbsp; &nbsp; &nbsp; &nbsp; context: Context,&nbsp; &nbsp; &nbsp; &nbsp; message: String,&nbsp; &nbsp; &nbsp; &nbsp; positiveText: String,&nbsp; &nbsp; &nbsp; &nbsp; isUa: Boolean,&nbsp; &nbsp; &nbsp; &nbsp; callback: OnUserInformedCallback): Dialog {&nbsp; &nbsp; val dialog = Dialog(context, android.R.style.Theme_Translucent_NoTitleBar)&nbsp; &nbsp; dialog.setContentView(R.layout.alert_dialog_layout)&nbsp; &nbsp; dialog.window?.setGravity(Gravity.CENTER)&nbsp; &nbsp; dialog.setCancelable(true)&nbsp; &nbsp; dialog.setCanceledOnTouchOutside(true)&nbsp; &nbsp; val tvOk: TextView = dialog.findViewById(R.id.visit)&nbsp; &nbsp; val tvMsg: TextView = dialog.findViewById(R.id.mess)&nbsp; &nbsp; tvMsg.text = message&nbsp; &nbsp; tvOk.text = positiveText&nbsp; &nbsp; dialog.findViewById<TextView>(R.id.cancel).setOnClickListener {&nbsp; &nbsp; &nbsp; &nbsp; callback.negative()&nbsp; &nbsp; }&nbsp; &nbsp; tvOk.setOnClickListener {&nbsp; &nbsp; &nbsp; &nbsp; callback.positive()&nbsp; &nbsp; }&nbsp; &nbsp; dialog.create()&nbsp; &nbsp; return dialog}在 java 中用于默认对话框private AlertDialog getDialog(Activity context, String message, OnUserInformedCallback&nbsp; callbac) {&nbsp; &nbsp; return new AlertDialog.Builder(context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle(R.string.app_name).setMessage(message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCancelable(true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setPositiveButton(android.R.string.yes, (dialog12, which) -> callbac.positive())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setNegativeButton(android.R.string.yes, (dialog1, which) -> callbac.positive())&nbsp; &nbsp; &nbsp; &nbsp; .create();}

Qyouu

是的,所以如果您没有使用某些 API 来解析信息,或者正在使用局部变量,我建议您在 onClickListener() 方法中执行任何您想执行的功能。问题是您使用的是 CustomDialog,它本身扩展了 Dialog 类。但不是使用它,而是创建一个新的警报对话框并构建它。您关闭它,但被关闭的对话框不是您的自定义对话框类,而是您在构造函数中创建的构建器对话框。即使您为此进行了修复,也会引入不必要的并发症。我建议您在 onClickListener() 函数中创建 Intent。这样做的方法是更改您的构造函数以支持回调侦听器。简而言之,当您收听的对话是另一个对话时,您不能只添加一个 onDismissListener() 。您可以做的是在用户将对话框作为特殊情况关闭时传入您想要执行的功能。见下文。所以,首先,像这样修改你的构造函数:public CustomDialog(Context context, String title, String message,&nbsp;DialogInterface.OnClickListener listener) {&nbsp; &nbsp; &nbsp;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&nbsp; &nbsp; &nbsp; &nbsp; .setCustomTitle(textView)&nbsp; &nbsp; &nbsp; &nbsp; .setMessage(message)&nbsp; &nbsp; &nbsp; &nbsp; .setPositiveButton("Ok", listener);AlertDialog customDialog = builder.show();TextView messageText = customDialog.findViewById(android.R.id.message);if (messageText != null) {&nbsp; &nbsp; messageText.setGravity(Gravity.CENTER);&nbsp; &nbsp; messageText.setTextColor(Color.GRAY);} else {&nbsp; &nbsp; Log.w(TAG, "messageText is null");}您所做的是您过去创建一个新的 onClickListener() 并传入监听器参数的地方。转到您的 MainActivity 或您创建自定义对话框的位置。这样做:CustomDialog customDialog = new CustomDialog(FirstActivity.this, "Title", "Message",&nbsp;new DialogInterface.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Do your functionality here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Intent intent = new Intent(context, activity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Add any flags if you want&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;context.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Or you can simply do context.finish();&nbsp; &nbsp; }});当您不想传递 onClickListener() 时(意思是当您不想进行finish()该活动时)传入null。它会起作用。如果这不是您想要的,请告诉我,我会修复它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java