Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从MotionEvent X,Y获取视图_Android_Events_View - Fatal编程技术网

Android 从MotionEvent X,Y获取视图

Android 从MotionEvent X,Y获取视图,android,events,view,Android,Events,View,在Activity.dispatchTouchEvent(MotionEvent ev)中,是否有方法获取对正在触摸的视图的引用 我需要隐藏一个自定义的RelativeLayout“键盘”,如果用户点击的地方不是EditText。由于我在活动中使用了许多片段,每个片段都有许多视图组,每个组都有自己的许多视图,其中一些是EditText,因此在每个子视图或子视图组上设置onTouchEvent将是太多的工作 在MainActivity中,我认为这样做是一个好办法 @Override public

在Activity.dispatchTouchEvent(MotionEvent ev)中,是否有方法获取对正在触摸的视图的引用

我需要隐藏一个自定义的RelativeLayout“键盘”,如果用户点击的地方不是EditText。由于我在活动中使用了许多片段,每个片段都有许多视图组,每个组都有自己的许多视图,其中一些是EditText,因此在每个子视图或子视图组上设置onTouchEvent将是太多的工作

在MainActivity中,我认为这样做是一个好办法

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mKeyboardLike.getVisibility() == View.VISIBLE) {
        if (!isTouchedEditText(ev.getRawX(), ev.getRawY())) {
            mKeyboardLike.setVisibility(View.GONE);
        }
    }
    return super.dispatchTouchEvent(ev);
}
private boolean isTouchedEditText(int x, int y){//viewFound instanceOf EditText; return true;}

我认为这应该对你有所帮助:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int x = Math.round(ev.getX());
    int y = Math.round(ev.getY());
    for (int i = 0; i < getChildCount(); i++) {
        if (isEditText(x, y, getChildAt(i))) {
            return true;
        }
    }

    return true;
}

public boolean isEditText(int x, int y, View view) {
    if (view instanceof ViewGroup) {
        for (int i = 0; i < getChildCount(); i++) {
            if (isEditText(x, y, ((ViewGroup) view).getChildAt(i))) {
                return true;
            }
        }
    } else if (view instanceof EditText) {
        if (x > view.getLeft() && x < view.getRight() && y > view.getTop() && y < view.getBottom()) {
            return true;
        }
    }
    return false;
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent ev){
intx=Math.round(ev.getX());
int y=Math.round(ev.getY());
对于(int i=0;iview.getLeft()&&xview.getTop()&&y