使用自定义对话框时无法使用 onDismiss() - Android

发布于 2024-10-16 06:59:08 字数 866 浏览 2 评论 0原文

我正在开发一个小程序,我需要添加一个自定义对话框,在关闭时将一些信息传递给调用活动。 我扩展了对话框类,当我尝试在关闭时捕获自定义对话框时,使用 onDismiss 侦听器,它永远不会到达它,因为我使用了自定义对话框。

这是我的活动的一部分 -

    .
    .
    .
       attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.show();

(属性是扩展对话框类的类的名称)。

这是我在对话框完成时设置的事件侦听器 -

    customizeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Log.v("LOG_CAT",attributes.selectedIndexes.get(0) + " " + attributes.selectedIndexes.get(1) + " " + attributes.selectedIndexes.get(2) + " " + attributes.selectedIndexes.get(3) + " " + attributes.selectedIndexes.get(5) + " ");
    }

});

我知道我做错了,我只是不知道如何修复它。

我真的很感激任何有关这个问题的帮助。

谢谢!

I'm working on a little program, and I need to add a custom dialog that passes some info to the calling acitivity when it closes.
I extended the dialog class, and when I try to capture the custom dialog when it closes,using an onDismiss listener, it never reaches it because I used a custom dialog.

This is part of my activity -

    .
    .
    .
       attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.show();

(The attributes being the name of the class that extends the dialog class).

Here is the event listener I set up when the dialog finishes -

    customizeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Log.v("LOG_CAT",attributes.selectedIndexes.get(0) + " " + attributes.selectedIndexes.get(1) + " " + attributes.selectedIndexes.get(2) + " " + attributes.selectedIndexes.get(3) + " " + attributes.selectedIndexes.get(5) + " ");
    }

});

I know i'm doing it wrong,I just don't know how to fix it.

I would really appreciate any help with this problem.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

软甜啾 2024-10-23 06:59:09

我倾向于让我的活动实现这样的监听器......

public class MyActivity extends Activity
    implements DialogInterface.OnDismissListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.setOnDismissListener(this);
        customizeDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Do whatever
    }
}

I tend to have my activity implement listeners like this...

public class MyActivity extends Activity
    implements DialogInterface.OnDismissListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.setOnDismissListener(this);
        customizeDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Do whatever
    }
}
韬韬不绝 2024-10-23 06:59:09

您可以让您的调用活动实现一个自定义侦听器接口,该接口在对话框关闭时调用:

public interface MyDialogListener {
    void OnCloseDialog();
}

public class MyActivity implements MyDialogListener {
    public void SomeMethod() {
        MyDialog myDialog = new MyDialog(this, this);
        myDialog.show();
    }

    public void OnCloseDialog() {
        // Do whatever you want to do on close here
    }

}

public class MyDialog extends Dialog {
    MyDialogListener mListener;

    public MyDialog (Context context, MyDialogListener listener) {
        super(context, R.style.Dialog);
        mListener = listener;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.CloseButton:
                mListener.OnCloseDialog();
                dismiss()
                break;
            default:
                //...
        }
    }
}

如果您想在除关闭之外的任何其他时间将内容发送回调用者,这尤其有用。

You could have your calling activity implement a custom listener interface that is called when the dialog closes:

public interface MyDialogListener {
    void OnCloseDialog();
}

public class MyActivity implements MyDialogListener {
    public void SomeMethod() {
        MyDialog myDialog = new MyDialog(this, this);
        myDialog.show();
    }

    public void OnCloseDialog() {
        // Do whatever you want to do on close here
    }

}

public class MyDialog extends Dialog {
    MyDialogListener mListener;

    public MyDialog (Context context, MyDialogListener listener) {
        super(context, R.style.Dialog);
        mListener = listener;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.CloseButton:
                mListener.OnCloseDialog();
                dismiss()
                break;
            default:
                //...
        }
    }
}

This is especially useful if you want to send stuff back to the caller at any other time besides on dismissal.

陌若浮生 2024-10-23 06:59:09

如果您想在对话框内进行某种保存,则必须使用 onDicmissListener 因为对于自定义对话框,默认情况下不会调用 onDismiss

public class CustomDialog extends Dialog implements DialogInterface.OnDismissListener {

    public CustomDialog(Context context) {
        super(context);
        setupLayout(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        setupLayout(context);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setupLayout(context);
    }

    private void setupLayout(Context context) {
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.FILL_PARENT;
        getWindow().setAttributes(params);

        setOnDismissListener(this);

        loadPreferences();
    }

    private void loadPreferences() {
      // ...
    }

    private void savePreferences() {
       // ...
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        savePreferences();
    }
}

And if you want to have some sort of saving inside the dialog, again, you have to use onDicmissListener since for custom dialogs onDismiss is not called by default:

public class CustomDialog extends Dialog implements DialogInterface.OnDismissListener {

    public CustomDialog(Context context) {
        super(context);
        setupLayout(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        setupLayout(context);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setupLayout(context);
    }

    private void setupLayout(Context context) {
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.FILL_PARENT;
        getWindow().setAttributes(params);

        setOnDismissListener(this);

        loadPreferences();
    }

    private void loadPreferences() {
      // ...
    }

    private void savePreferences() {
       // ...
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        savePreferences();
    }
}
Spring初心 2024-10-23 06:59:09

如果您使用自定义对话框并且无法关闭它,请尝试以下代码。
这对我有用。

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           dialog.dismiss();
        }
    }, 1500);

If you are using custom dialog and can't dismiss it, try below code.
It worked for me.

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           dialog.dismiss();
        }
    }, 1500);
往事风中埋 2024-10-23 06:59:09

要记住的一件事是,OnDismissListener 正在侦听子进程的关闭。客户对话框的父级需要 onDismissListener,而不是对话框本身。

“用于允许对话框创建者在对话框关闭时运行一些代码的接口。”

One thing to remember is that an OnDismissListener is listening for the dismiss of the child processes. The parent of your customer dialog needs the onDismissListener, not the dialog itself.

"Interface used to allow the creator of a dialog to run some code when the dialog is dismissed."

墨落成白 2024-10-23 06:59:09

要在 CustomDialog 类中添加对话框:

public class MessageBoxDialog extends Dialog implements DialogInterface.OnDismissListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         ...
         setOnDismissListener(this);
         ...
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {

    }
}

To add dialog inside CustomDialog class:

public class MessageBoxDialog extends Dialog implements DialogInterface.OnDismissListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         ...
         setOnDismissListener(this);
         ...
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文