开发者

Starting an Android activity as a dialog without using a theme modifier

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 ?


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.


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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜