查看“查看"之外的点击 [英] Check click outside of View
问题描述
这是我的布局:
<?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设置为父视图和子视图:
I set a onclicklistener to parent and child view:
linearLayout.setOnClickListener(myOnlyhandler);
videoView.setOnClickListener(myOnlyhandler);
我仅获得视频观看的点击事件. 我想要知道的是用户是否单击视频视图或在外部(父级)单击并执行不同的操作.如何获得这种行为?
I get only click events for video view. What I want is to know if the user clicks into the video view or outside (parent) and do different actions. How do I get this behavior?
谢谢 塔塔
推荐答案
前一段时间,我实现了一个提供此类功能的Activity
基类.您只需注册一个自定义OnTouchOutsideViewListener
,当用户在安装了侦听器后在视图外触摸时便会得到通知.只需在您的活动实例上调用setOnTouchOutsideViewListener
.
A while ago I've implemented an Activity
base class that offers such functionality. You simply register a custom OnTouchOutsideViewListener
that gets notified once the user touches outside your view after having installed a listener. Simply call setOnTouchOutsideViewListener
on your activity instance.
您可以将此代码粘贴到现有活动类中,或创建一个基础活动类,以在整个项目中重复使用.
You can paste this code into your existing activity class or create a base activity class that you can reuse throughout your project.
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);
}
}
这篇关于查看“查看"之外的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!