仅使用 Context 而不是 Activity 实例显示对话框

Show dialog only using Context instead of Activity instance(仅使用 Context 而不是 Activity 实例显示对话框)
本文介绍了仅使用 Context 而不是 Activity 实例显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 Activity 实例,我可以显示对话框,但是当我使用 Context 或 Application Context 实例时,对话框没有显示.

I could show dialog if I uses an Activity instance but when I uses Context or Application Context instance Dialog is not showing.

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(title);
            builder.setMessage(msg);

            if (null != positiveLabel) {
                builder.setPositiveButton(positiveLabel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onOk();
                        }
                    }
                });
            }

            if (null != negativeLable) {
                builder.setNegativeButton(negativeLable, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onCancel();
                        }
                    }
                });
            }

            builder.create().show();

你能不能给我一个解决方案来显示对话框而不使用 Activity 实例

Can you please give me a solution to show dialog without using Activity instance

推荐答案

这个问题也是我最近遇到的问题,没有Activity实例就不能创建对话框.getApplicationContext() 调用也不起作用.我这样做的方法是从一个活动调用创建对话框的方法,并传递this",即对该活动的引用作为参数.

The problem is something I faced recently too, you cant create a dialog without and activity instance. getApplicationContext() call doesn't work too. The way I did this is to make the call to a method that creates the dialog, from an activity, and pass "this" i.e. the reference to that activity as a parameter.

如果您打算重用此代码,作为可重用组件或作为在多个位置创建对话框的机制,请创建一个基本活动类并在其中包含此方法,并根据需要在子类活动中使用它.

If you are going to reuse this code, as a reusable component or as a mechanism to create dialogs at multiple places, create a base activity class and have this method in there, and use it in sub-classed activities as needed.

这篇关于仅使用 Context 而不是 Activity 实例显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)