在不使用主题修改器的情况下以对话框形式启动 Android 活动

发布于 2024-09-24 06:07:34 字数 328 浏览 6 评论 0原文

我想以对话框的形式启动一个活动,这可以简单地通过以下方式完成:

<activity android:theme="@android:style/Theme.Dialog">

但我想控制对话框,所以我必须以编程方式完成。基本上我想修改对话框的这个属性:

mCanceledOnTouchOutside = true

这将使对话框在触摸其边界之外时自行取消。 (基本上我想复制弹出行为)。问题是我不能简单地创建一个对话框并设置它的布局,因为我需要调用活动(初始化数据集)

这可能吗?

I want to start an activity as a dialog, which can be simply done by:

<activity android:theme="@android:style/Theme.Dialog">

But I want to do control the dialog, so I've to do it programmatically. Basically I want to modify this property of dialog:

mCanceledOnTouchOutside = true

This will make the dialog cancel itself when touched outside of it's bounds. (Basically I want to replicate the popup behavior). The issue is I can't simply create a dialog and set it's layout since I need a call to activity (to initialized datasets)

Is this possible ?

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

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

发布评论

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

评论(3

嘿哥们儿 2024-10-01 06:07:35

从那以后这并没有真正实现,因为活动不是对话框。通过设置主题,您所做的就是使活动直观地使用对话框的主题。它仍然是一个活动,但是,将是所有正常活动的行为。换句话说,不存在具有关联行为的 Dialog 对象。

This doesn't really make since, because an activity is not a dialog. By setting the theme, all you are doing is causing the activity to visually use the theme of a dialog. It is still an activity, however, will all of the normal activity behavior. In other words, there is no Dialog object with its associated behavior.

混吃等死 2024-10-01 06:07:35

我只是想在我的对话框样式活动之一上重现此 cancelOutside 对话框行为。为此,我提取了处理触摸事件的对话框源代码(Dialog.java):

public boolean onTouchEvent(MotionEvent event) {
    if (mCancelable && mCanceledOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(event)) {
        cancel();
        return true;
    }
    return false;
}

private boolean isOutOfBounds(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop) || (x > (decorView.getWidth()+slop)) || (y > (decorView.getHeight()+slop));
}

我将其复制粘贴到我的对话框样式活动中并更改了一些小东西:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(event)) {
        finish();
        return true;
    }
    return false;
}

private boolean isOutOfBounds(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(this).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > decorView.getHeight() + slop));
}

它工作正常!

I just wanted to reproduce this cancelOutside Dialog behavior on one of my Dialog style Activity. For this I extracted the Dialog source code (Dialog.java) handling the touch event:

public boolean onTouchEvent(MotionEvent event) {
    if (mCancelable && mCanceledOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(event)) {
        cancel();
        return true;
    }
    return false;
}

private boolean isOutOfBounds(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop) || (x > (decorView.getWidth()+slop)) || (y > (decorView.getHeight()+slop));
}

I Copy paste it in my Dialog style activity and changed small things:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(event)) {
        finish();
        return true;
    }
    return false;
}

private boolean isOutOfBounds(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(this).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > decorView.getHeight() + slop));
}

It works fine!

请别遗忘我 2024-10-01 06:07:35

您可以在 values/styles.xml 文件(或任何您想要的名称)中定义扩展 Theme.Dialog 的您自己的样式。查看 http://developer.android.com/guide/topics/ ui/themes.html#继承

我不确定这是否有效,但也许可以尝试以下方法:

<style name="DialogCancelOnTouchOutside" parent="@android:style/Theme.Dialog">
    <item name="android:canceledOnTouchOutside">true</item>
</style>

You can define your own style that extends Theme.Dialog in a values/styles.xml file (or whatever you want to call it). Check out http://developer.android.com/guide/topics/ui/themes.html#Inheritance.

I'm not sure if this will work, but maybe try something like:

<style name="DialogCancelOnTouchOutside" parent="@android:style/Theme.Dialog">
    <item name="android:canceledOnTouchOutside">true</item>
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文