按钮调用的对应视图怎么找

怎么找到按钮事件对应的视图,RouteSearchResultActivity里吗?怎么没找到

img

img

activity对应的layout里面就是

你想在点击事件里面直接拿到RouteSearchResultActivity的view控件?

打断点调试呗,注意一下变量的变化

activity对应的layout

layout布局ctrl加鼠标单击即可

题主是想深入了解机制吗,通过遍历可以找:例子

private View getViewByPosition(View view, int x,  int y) {
        if (view == null) {
            return null;
        }
        int[] location = new int[2];
        view.getLocationInWindow(location);

        int left = location[0];
        int top  = location[1];
        int right = left + view.getWidth();
        int bottom = top + view.getHeight();

        if (view instanceof ViewGroup) { //当前是ViewGroup容器
            int childCount = ((ViewGroup)view).getChildCount();
            //深度优先, 从最后一个子节点开始遍历,如果找到则返回。 先递归判断子View
            if (childCount > 0) {
                for (int i = childCount - 1; i >= 0; i--) {
                    View topView = getViewByPosition(((ViewGroup)view).getChildAt(i), x, y);
                    if (topView != null) {
                        return topView;
                    }
                }
            }

            //子View都没找到匹配的, 再判断自己
            if (left < x && top < y && right > x && bottom > y) {
                return view;   //当前ViewGroup就是顶层View
            } else {
                return null; //没找到匹配的
            }
        } else { //当前是View
            if (left < x && top < y && right > x && bottom > y) {
                return view;   //当前ViewGroup就是顶层View
            } else {
                return null; //没找到匹配的
            }
        }
    }