prevent"对话"从背景活动一下外边界时互动活动互动、外边、背景、prevent

2023-09-12 05:10:30 作者:出租半张床

我创建看起来像一个对话的活动。

I'm creating an activity that looks like a dialog.

下面是风格:

<style name="TablesDialogActivity" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowBackground">@drawable/frame_background_left</item>
</style>

这是该活动的onCreate():

protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        super.onCreate(savedInstanceState);
}

同时,也是活动的触摸拦截器里面的:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        finish();
        return true;
    }
    return false;
}

这pretty的作品不多,活动结束的对话框边界之外的触摸,但它也与后台活动这是坏的按钮交互。对文档的的onTouchEvent 说,如果你消耗的触摸事件,你应该返回true。我返回true,但它似乎这种方式确实。

It pretty much works, activity finishes on touch outside dialog bounds, but it also interacts with buttons on the background activity which is bad. Documentation on the onTouchEvent says that you should return true if you consumed the touch event. I return true, but it doesn't seem that way.

推荐答案

注意,你需要从问题过于整个code。

在除了我的解决方案背景活动在(或应用程序的只是基本活动)我说:

In addition to my solution inside background activity (or just base activity of your application) I added:

private FrameLayout touchInterceptor;

@Override
protected void onPause() {
    if (touchInterceptor.getParent() == null) {
        ((ViewGroup) findViewById(android.R.id.content)).addView(touchInterceptor);
    }
    super.onPause();
}

@Override
protected void onResume() {
    ((ViewGroup) findViewById(android.R.id.content)).removeView(touchInterceptor);
    super.onResume();
}

而在的onCreate()

    // For intercepting clicks from dialog like activities
    touchInterceptor = new FrameLayout(this);
    touchInterceptor.setClickable(true);

现在就像一个魅力! :)

Now works like a charm! :)

 
精彩推荐
图片推荐