定时非模式对话框

发布于 2024-10-01 11:43:06 字数 275 浏览 10 评论 0原文

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

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

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

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

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

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?

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.

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?

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

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

发布评论

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

评论(3

戏蝶舞 2024-10-08 11:43:06

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

一个例子:

<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 。

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

    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);

这是该对话框的布局:

<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>

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

An example:

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

Edited:

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>
抠脚大汉 2024-10-08 11:43:06

只需将 FLAG_NOT_TOUCH_MODAL 标志添加到您的对话框中

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);  

just add FLAG_NOT_TOUCH_MODAL flag to your dialog

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);  
っ〆星空下的拥抱 2024-10-08 11:43:06

我的实现有点黑客化,但也允许后台窗口捕获按钮按下的情况

_wm = this.getWindowManager();
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_view = layoutInflater.inflate(R.layout.notification, null);

_params = new WindowManager.LayoutParams();
_params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.flags = 
    // this is to keep button presses going to the background window
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
    // this is to enable the notification to recieve touch events
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
// background transparant
_params.format = PixelFormat.TRANSLUCENT;
_gravity = Gravity.TOP;//Gravity.BOTTOM;

_wm.addView(_view, _params);

My implementation which was a little more hackish but also allows button presses to be caught by background window

_wm = this.getWindowManager();
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_view = layoutInflater.inflate(R.layout.notification, null);

_params = new WindowManager.LayoutParams();
_params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
_params.flags = 
    // this is to keep button presses going to the background window
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
    // this is to enable the notification to recieve touch events
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
// background transparant
_params.format = PixelFormat.TRANSLUCENT;
_gravity = Gravity.TOP;//Gravity.BOTTOM;

_wm.addView(_view, _params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文