Android 单例对话框

发布于 2024-10-04 18:31:12 字数 148 浏览 2 评论 0原文

我有一个处理大量进度对话框的 Android 应用程序。我必须为每个活动创建一个单独的对话框。

对话框创建在构造时将活动(上下文)作为参数。

有没有一种方法可以创建单个对话框(与应用程序而不是活动相关)并在不同的活动中显示它,这样我就不必重复创建它。

I have android application which deals with lot of progress dialogs.I have to create a separate dialog for each activity.

Dialog creation takes a activity(context) as parameter while constructing.

Is there a way by which I can create a single dialog (which is tied to application and not the activity) and show it in different activity so that I do not have to create it repeatedly.

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

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

发布评论

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

评论(5

小猫一只 2024-10-11 18:31:12

在 Utill 帮助器类中声明 showProgressDialoghideProgressDialog,如以下代码片段所示,

public static ProgressDialog showProgressDialog(Context context) {
        ProgressDialog pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        return pDialog;
    }

    public static void hideProgressDialog(ProgressDialog pDialog) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

然后从需要显示 ProgressDialog 的活动中调用,例如在 onPreExecute() 方法,如下面的代码片段所示

ProgressDialog pDialog = Util.showProgressDialog(this);

,并使用以下代码隐藏 ProgressDialog

 Util.hideProgressDialog(pDialog);

Declare showProgressDialog and hideProgressDialog in Utill helper class as shown in following code snippet

public static ProgressDialog showProgressDialog(Context context) {
        ProgressDialog pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        return pDialog;
    }

    public static void hideProgressDialog(ProgressDialog pDialog) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

Then call from activity where you need to show the ProgressDialog for example in onPreExecute() method of AsyncTask class as shown in below code snippet

ProgressDialog pDialog = Util.showProgressDialog(this);

and use following code to hide the progressDialog

 Util.hideProgressDialog(pDialog);
飘落散花 2024-10-11 18:31:12

将对话框代码放入辅助类的接收上下文的静态方法中可能是最好的方法。

Putting dialog code into a helper class's static method receiving a Context maybe the best way.

何以心动 2024-10-11 18:31:12

不幸的是,没有。您必须将对话框附加到活动,否则您的应用程序将容易崩溃。例如,您可能会遇到类似 android.view.WindowManager$BadTokenException 的异常。

Unfortunately, no. You have to attach the dialog to an activity, otherwise your application will tend to crash. You could get exceptions like android.view.WindowManager$BadTokenException for instance.

几味少女 2024-10-11 18:31:12

这并不是你问题的真正答案,但也许我的想法对你有帮助。我创建了一个 BaseActivity,有一个成员对话框、作为成员的活动上下文和两个方法,用于显示和隐藏进度对话框。所有其他活动都是从它延伸出来的。

It's not really the answer for your question, but maybe my idea helps you. I've created a BaseActivity, there is a member dialog, activity context as member and two methods, to show and to hide progess dialog. All other activities are extended from it.

赴月观长安 2024-10-11 18:31:12

如果您只想在对话框中定义 ProgressBar,请

    public class ProgressDialog {
    private Dialog dialog;
    private static ProgressDialog mInstance;

    public static synchronized ProgressDialog getInstance() {
        if (mInstance == null) {
            mInstance = new ProgressDialog();
        }
        return mInstance;
    }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new Dialog(context,R.style.ProgressDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(false);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

在布局文件夹中定义此类和 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/progress"
        android:id="@+id/progress" />

</LinearLayout>

,并在 style.xml 中定义此样式

<style name="ProgressDialog" parent="Animation.AppCompat.Dialog">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

If you want ProgressBar only in the dialog define this class

    public class ProgressDialog {
    private Dialog dialog;
    private static ProgressDialog mInstance;

    public static synchronized ProgressDialog getInstance() {
        if (mInstance == null) {
            mInstance = new ProgressDialog();
        }
        return mInstance;
    }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new Dialog(context,R.style.ProgressDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(false);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

and this XML in layout folder

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/progress"
        android:id="@+id/progress" />

</LinearLayout>

and this style in style.xml

<style name="ProgressDialog" parent="Animation.AppCompat.Dialog">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文