定时无模式对话 [英] timed modeless dialog

查看:20
本文介绍了定时无模式对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以显示无模式对话框 - 一个允许用户与对话框之前屏幕上的任何内容进行交互的对话框,但还允许用户在按下时与对话框进行交互?

Is there any way to show a modeless dialog--a dialog that allows the user to interact with whatever was on the screen before the dialog but also allows the user to interact with the dialog if pressed?

我知道 Toasts,但它们不允许与弹出窗口交互.

I know of Toasts, but they don't allow interaction with the popup.

我知道对话框,但它们是模态的,不允许与背景交互.

I know of Dialogs, but they're modal and don't allow interaction with the background.

我知道通知,但我想要在屏幕上可见的东西.

I know of Notifications, but I want something that is visibile on screen.

我基本上希望能够玩游戏或其他东西,然后出现一个弹出窗口,提示我有一封新电子邮件或其他东西.我可以点击它来查看我的电子邮件,但如果我只想继续玩我的游戏,我可以等待它消失.这在 Android 中可行吗?

I basically want to be able to be playing a game or something and a popup appears that I have a new email or something. I can click it to view my email, but I can wait for it to go away if I just want to continue playing my game. Is this possible in Android?

推荐答案

是的,创建一个样式为 Theme.Dialog 的 Activity.这是一个正常的活动,看起来像一个对话框,同时是无模式的并接受事件.

Yes, create an Activity with style Theme.Dialog. This is a normal activity which looks like a dialog, while being modeless and accepting events.

一个例子:

<activity android:name=".activity.dialog.PhotoDialog"
          android:label="@string/photo_dialog_title"
          android:theme="@android:style/Theme.Dialog"/>

已编辑:

确实 Theme.Dialog 模糊了底层活动并使其无法访问.我在这里有类似的要求,我必须显示带有文本和取消按钮的上传进度对话框.主要问题在于设置 WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 和重置 WindowManager.LayoutParams.FLAG_DIM_BEHIND.

Indeed Theme.Dialog blurs the underlying activity and makes it unaccessible. I had a similar requirement here I had to show upload progress dialog with text and cancel button. The main catch is in setting WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL and resetting WindowManager.LayoutParams.FLAG_DIM_BEHIND.

创建了一个包含自定义内容的对话框:

Created a Dialog with custom content:

    if (progressDialog == null) {
            progressDialog = new Dialog(activityRequestingProgressDialog);
            progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            progressDialog.setContentView(R.layout.progress_upload);
            progressBar = (ProgressBar) progressDialog.findViewById(R.id.progressBar);
            progressText = (TextView) progressDialog.findViewById(R.id.progressText);
            progressText.setText("0 %");
            progressText.setTextSize(18);
            Button buttonCancel = (Button) progressDialog.findViewById(R.id.btnCancel);
            buttonCancel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    cancelProgressDialog();
                    stopUpload("Upload cancelled.");
                }
            });
            Window window = progressDialog.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setGravity(Gravity.BOTTOM);
            progressDialog.show();
        }

        progressText.setText(text);
        progressBar.setProgress(percent);

这是这个对话框的布局:

And this is the layout for this Dialog:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/progressDialog"
          android:orientation="vertical"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:layout_centerVertical="true">

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:textSize="18sp"
          android:padding="10dp"
          android:text="@string/progress_title"/>

<LinearLayout android:id="@+id/progressDialog"
              android:orientation="horizontal"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:padding="10dp"
              android:layout_centerVertical="true">

    <ProgressBar android:id="@+id/progressBar"
                 android:layout_width="150dp"
                 android:layout_height="34dp"
                 android:paddingRight="10dp"
                 android:max="100"
                 android:progress="0"
                 android:fadingEdge="vertical"
                 style="?android:attr/progressBarStyleHorizontal"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:id="@+id/progressText"
              android:paddingRight="10dp"/>

    <Button android:layout_height="40dp"
            android:layout_width="80dp"
            android:id="@+id/btnCancel"
            android:text="@string/dialog_cancel"/>

</LinearLayout>
</LinearLayout>

这篇关于定时无模式对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆