<i id='PUtxa'><tr id='PUtxa'><dt id='PUtxa'><q id='PUtxa'><span id='PUtxa'><b id='PUtxa'><form id='PUtxa'><ins id='PUtxa'></ins><ul id='PUtxa'></ul><sub id='PUtxa'></sub></form><legend id='PUtxa'></legend><bdo id='PUtxa'><pre id='PUtxa'><center id='PUtxa'></center></pre></bdo></b><th id='PUtxa'></th></span></q></dt></tr></i><div id='PUtxa'><tfoot id='PUtxa'></tfoot><dl id='PUtxa'><fieldset id='PUtxa'></fieldset></dl></div>
    <tfoot id='PUtxa'></tfoot>

      • <bdo id='PUtxa'></bdo><ul id='PUtxa'></ul>
    1. <small id='PUtxa'></small><noframes id='PUtxa'>

      <legend id='PUtxa'><style id='PUtxa'><dir id='PUtxa'><q id='PUtxa'></q></dir></style></legend>

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

      时间:2023-08-28

        <tfoot id='iAxoH'></tfoot>
          <bdo id='iAxoH'></bdo><ul id='iAxoH'></ul>

            <tbody id='iAxoH'></tbody>
        • <small id='iAxoH'></small><noframes id='iAxoH'>

          <legend id='iAxoH'><style id='iAxoH'><dir id='iAxoH'><q id='iAxoH'></q></dir></style></legend>

                <i id='iAxoH'><tr id='iAxoH'><dt id='iAxoH'><q id='iAxoH'><span id='iAxoH'><b id='iAxoH'><form id='iAxoH'><ins id='iAxoH'></ins><ul id='iAxoH'></ul><sub id='iAxoH'></sub></form><legend id='iAxoH'></legend><bdo id='iAxoH'><pre id='iAxoH'><center id='iAxoH'></center></pre></bdo></b><th id='iAxoH'></th></span></q></dt></tr></i><div id='iAxoH'><tfoot id='iAxoH'></tfoot><dl id='iAxoH'><fieldset id='iAxoH'></fieldset></dl></div>
                本文介绍了仅使用 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 实例显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Appcompat 警报对话框操作按钮背景 按下状态 下一篇:Windows 10 升级后 Genymotion 错误

                相关文章

                最新文章

                1. <legend id='ZSMJf'><style id='ZSMJf'><dir id='ZSMJf'><q id='ZSMJf'></q></dir></style></legend>
                  <i id='ZSMJf'><tr id='ZSMJf'><dt id='ZSMJf'><q id='ZSMJf'><span id='ZSMJf'><b id='ZSMJf'><form id='ZSMJf'><ins id='ZSMJf'></ins><ul id='ZSMJf'></ul><sub id='ZSMJf'></sub></form><legend id='ZSMJf'></legend><bdo id='ZSMJf'><pre id='ZSMJf'><center id='ZSMJf'></center></pre></bdo></b><th id='ZSMJf'></th></span></q></dt></tr></i><div id='ZSMJf'><tfoot id='ZSMJf'></tfoot><dl id='ZSMJf'><fieldset id='ZSMJf'></fieldset></dl></div>
                  1. <small id='ZSMJf'></small><noframes id='ZSMJf'>

                    <tfoot id='ZSMJf'></tfoot>
                      <bdo id='ZSMJf'></bdo><ul id='ZSMJf'></ul>