Android 根据坐标获取控件方法
2015-01-14 14:07
411 查看
/** * 根据坐标获取相对应的子控件<br> * 在Activity使用 * * @param x坐标 * @param y坐标 * @return 目标View */ public View getViewAtActivity(int x, int y) { // 从Activity里获取容器 View root = getWindow().getDecorView(); return findViewByXY(root, x, y); } /** * 根据坐标获取相对应的子控件<br> * 在重写ViewGroup使用 * * @param x坐标 * @param y坐标 * @return 目标View */ public View getViewAtViewGroup(int x, int y) { return findViewByXY(this, x, y); } private View findViewByXY(View view, int x, int y) { View targetView = null; if (view instanceof ViewGroup) { // 父容器,遍历子控件 ViewGroup v = (ViewGroup) view; for (int i = 0; i < v.getChildCount(); i++) { targetView = findViewByXY(v.getChildAt(i), x, y); if (targetView != null) { break; } } } else { targetView = getTouchTarget(view, x, y); } return targetView; } private View getTouchTarget(View view, int x, int y) { View targetView = null; // 判断view是否可以聚焦 ArrayList<View> TouchableViews = view.getTouchables(); for (View child : TouchableViews) { if (isTouchPointInView(child, x, y)) { targetView = child; break; } } return targetView; } private boolean isTouchPointInView(View view, int x, int y) { int[] location = new int[2]; view.getLocationOnScreen(location); int left = location[0]; int top = location[1]; int right = left + view.getMeasuredWidth(); int bottom = top + view.getMeasuredHeight(); if (view.isClickable() && y >= top && y <= bottom && x >= left && x <= right) { return true; } return false; }
相关文章推荐
- Android 根据坐标获取控件方法
- android根据坐标获取相应控件,判断点是否在控件上
- Android获取控件坐标为0的原因及取得控件位置和两控件距离的方法
- [Android]在游戏的SurfaceView中获取输入与使用EditText,Spinner等控件的方法
- android 获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- Android 获取控件高度宽度三种方法
- Android 获取控件高度宽度三种方法,防止0的出现,计算空间宽度,文字宽度
- Android 根据坐标获取地址
- android获取屏幕宽高与获取控件宽高 方法总结
- Android 获取控件高度宽度三种方法,防止0的出现
- Android 获取控件高度宽度三种方法,防止0的出现
- Android 获取控件高度宽度三种方法,防止0的出现
- Android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- Android 简化获取控件的方法
- MFC 获取picture控件的鼠标点击坐标位置的方法
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- winform Chart控件 获取鼠标处坐标值方法
- Android中的坐标及各种获取尺寸方法的含义
- 获取DataGridView绑定BindingSource的数据表,并根据树控件点击进行筛选数据表,下拉控件自定义排序问题初步解决方法,解决了该行属于另一个表的错误提示.