简体   繁体   English

Android:无法取消ViewPager中片段中的自定义对话框

[英]Android: Cannot cancel custom dialog in a fragment in a viewpager

In my MainActivity, I have a fragment in a viewpager. 在我的MainActivity中,viewpager中有一个片段。 In this fragment I build a custom dialog. 在此片段中,我构建了一个自定义对话框。 The custom dialog can be cancel normally in other fragments which are called from MainActivity. 可以从MainActivity调用的其他片段中正常取消自定义对话框。 However, in this fragment, it cannot be cancel. 但是,在此片段中,它不能被取消。 I don't know what happened with my dialog. 我不知道我的对话发生了什么。 Please help me to check the code below. 请帮助我检查下面的代码。 Thanks in advance. 提前致谢。

    dialog = new Dialog(getActivity(), R.style.CustomDialogTheme);

    dialog.setContentView(R.layout.popup_login_sns);

    LinearLayout cancelButton = (LinearLayout) dialog.findViewById(R.id.cancelButtonSNS);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("GETBOOKMARK", "call dialog cancel");
            dialog.cancel();
        }
    });

    dialog.show();

Try with dismiss() 尝试使用dismiss()

 cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("GETBOOKMARK", "call dialog cancel");
            dialog.dismiss();
        }
    });

public void cancel ()

Cancel the dialog. 取消对话框。 This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered). 这本质上与调用dismiss()相同,但也会调用DialogInterface.OnCancelListener(如果已注册)。

public void dismiss ()

Dismiss this dialog, removing it from the screen. 退出此对话框,将其从屏幕上删除。 This method can be invoked safely from any thread. 可以从任何线程安全地调用此方法。 Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop(). 请注意,在关闭对话框时,您不应覆盖此方法进行清理,而应在onStop()中实现。

Here is code what is used, 这是使用的代码,

 final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog);
            dialog.show();
            ImageView gifView1 = (ImageView) dialog.findViewById(R.id.gifImageView);
            Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
            Button accecptBtn = (Button) dialog.findViewById(R.id.acceptButton);
            Button rateAppBtn = (Button) dialog.findViewById(R.id.rateButton);
            declineButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            accecptBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });

Just change your method dialog.cancel(); 只需更改您的方法dialog.cancel(); to dialog.dismiss(); dialog.dismiss();

 dialog = new Dialog(getActivity(), R.style.CustomDialogTheme);

        dialog.setContentView(R.layout.popup_login_sns);

        LinearLayout cancelButton = (LinearLayout) dialog.findViewById(R.id.cancelButtonSNS);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d("GETBOOKMARK", "call dialog cancel");
                    dialog.dismiss();
                }
            });

            dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM