如何在没有AlertDialog的自定义对话框中实现取消按钮?

时间:2022-09-02 13:32:41

i know, similar questions have been asked, and i've already looked through everything i could find, but i didn't find any answer to this problem.

我知道,类似的问题已被问到,我已经查看了我能找到的所有内容,但我没有找到任何答案。

Here's the Code:

这是代码:

    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = new Dialog(this);
        switch(id)
        {
            case R.layout.database_feed:
                dialog.setContentView(R.layout.database_feed);
                ((Button) dialog.findViewById(R.id.discard_button)).setOnClickListener(
                    new View.OnClickListener() 
                    {
                        //@Override
                        public void onClick(View v) 
                        {
                            //dialog.cancel();
                        }
                    }
                );
                break;
        }
        return dialog;
    }

I simply want to close the Dialog on a click on R.layout.database_feed button. But i don't have acces to the dialog within the onClick-method. I really feel confused.

我只想点击R.layout.database_feed按钮关闭对话框。但我没有访问onClick方法中的对话框。我真的很困惑。

I don't want to use an AlertDialog or a DialogBuilder, because there are other things in the Dialog that are difficult to implement in an AlertDialog or something. Also, i already know the solution to make a separate Activity for the Dialog - but actually i want to know how it works the way i'm trying here. Moreover i have already tried to use a DialogInterface.OnClickListener() but i can't use that in the setOnClickListener(...)-Method.

我不想使用AlertDialog或DialogBu​​ilder,因为在Dialog中还有其他东西很难在AlertDialog中实现。此外,我已经知道为对话框单独创建活动的解决方案 - 但实际上我想知道它是如何工作的,我正在尝试这里。此外,我已经尝试使用DialogInterface.OnClickListener()但我不能在setOnClickListener(...) - 方法中使用它。

Just cancelling the dialog can't be that hard... but i don't get it.

只是取消对话框不会那么难......但我不明白。

Any hint/help is appreciated!

任何提示/帮助表示赞赏!

Thx

谢谢

2 个解决方案

#1


4  

Change

更改

Dialog dialog = new Dialog(this);

to

final Dialog dialog = new Dialog(this);

Then you can access dialog in your onClick() method.

然后,您可以在onClick()方法中访问对话框。

#2


2  

Either store the 'dialog' as a class variable, or make it final in your method.

将“对话框”存储为类变量,或者在方法中将其设置为final。

#1


4  

Change

更改

Dialog dialog = new Dialog(this);

to

final Dialog dialog = new Dialog(this);

Then you can access dialog in your onClick() method.

然后,您可以在onClick()方法中访问对话框。

#2


2  

Either store the 'dialog' as a class variable, or make it final in your method.

将“对话框”存储为类变量,或者在方法中将其设置为final。