检查在视图外部的点击

4
这是我的布局:

<?xml version="1.0" encoding="utf-8"?><!-- Style Theme.Transparent -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutVideo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:weightSum="1"
    android:focusable="true"
    android:clickable="true">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:clickable="true"
        android:focusable="true" />

</LinearLayout>

我给父视图和子视图设置了onclicklistener:

linearLayout.setOnClickListener(myOnlyhandler);
videoView.setOnClickListener(myOnlyhandler);

我只收到视频视图的点击事件。 我想知道用户是在视频视图内部还是外部(父级)进行点击,并执行不同的操作。我该如何实现这个功能?

谢谢 Tata


请展示您的myOnlyhandler代码。 - Steve Benett
如果您在视频视图之外单击,您仍将获得videoView的点击事件吗? - suitianshi
1个回答

16

之前我实现了一个Activity基类,提供了这样的功能。您只需要注册一个自定义的OnTouchOutsideViewListener,在安装监听器后,用户触摸您的视图外部后会得到通知。只需在您的活动实例上调用setOnTouchOutsideViewListener即可。

您可以将此代码粘贴到现有的活动类中,或者创建一个基本活动类,可以在整个项目中重复使用。

class YourActivity extends Activity {

private View mTouchOutsideView;

private OnTouchOutsideViewListener mOnTouchOutsideViewListener;

/**
 * Sets a listener that is being notified when the user has tapped outside a given view. To remove the listener,
 * call {@link #removeOnTouchOutsideViewListener()}.
 * <p/>
 * This is useful in scenarios where a view is in edit mode and when the user taps outside the edit mode shall be
 * stopped.
 *
 * @param view
 * @param onTouchOutsideViewListener
 */
public void setOnTouchOutsideViewListener(View view, OnTouchOutsideViewListener onTouchOutsideViewListener) {
    mTouchOutsideView = view;
    mOnTouchOutsideViewListener = onTouchOutsideViewListener;
}

public OnTouchOutsideViewListener getOnTouchOutsideViewListener() {
    return mOnTouchOutsideViewListener;
}

@Override
public boolean dispatchTouchEvent(final MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // Notify touch outside listener if user tapped outside a given view
        if (mOnTouchOutsideViewListener != null && mTouchOutsideView != null
            && mTouchOutsideView.getVisibility() == View.VISIBLE) {
            Rect viewRect = new Rect();
            mTouchOutsideView.getGlobalVisibleRect(viewRect);
            if (!viewRect.contains((int) ev.getRawX(), (int) ev.getRawY())) {
                mOnTouchOutsideViewListener.onTouchOutside(mTouchOutsideView, ev);
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}

/**
 * Interface definition for a callback to be invoked when a touch event has occurred outside a formerly specified
 * view. See {@link #setOnTouchOutsideViewListener(View, OnTouchOutsideViewListener).}
 */
public interface OnTouchOutsideViewListener {

    /**
     * Called when a touch event has occurred outside a given view.
     *
     * @param view  The view that has not been touched.
     * @param event The MotionEvent object containing full information about the event.
     */
    public void onTouchOutside(View view, MotionEvent event);
}    

}

1
总体来说是个不错的答案。如果触摸事件发生在视图之外,你可能想要让dispatchTouchEvent()返回true,以便消耗触摸事件。另外,更常见的是在MotionEvent.ACTION_UP上触发。 - Rasmusob
2
这个答案简直完美! - Matan Dahan
运作得非常好!谢谢你。 - Dev4Life

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接