http://www.2cto.com/kf/201501/370226.html
http://www.2cto.com/kf/201501/370226.html
http://www.2cto.com/kf/201501/370226.html
http://www.2cto.com/kf/201501/370226.html
- Android 根据坐标获取控件方法
id="iframeu2597680_0" src="http://pos.baidu.com/lcbm?sz=650x180&rdid=2597680&dc=2&di=u2597680&dri=0&dis=0&dai=4&ps=329x96&coa=at%3D3%26rsi0%3D650%26rsi1%3D180%26pat%3D1%26tn%3DbaiduCustNativeAD%26rss1%3D%2523F9F9F9%26conBW%3D0%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0%26tft%3D0%26tlt%3D1%26ptbg%3D90%26piw%3D140%26pih%3D90%26ptp%3D1&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1475034420591&ti=Android%20%E6%A0%B9%E6%8D%AE%E5%9D%90%E6%A0%87%E8%8E%B7%E5%8F%96%E6%8E%A7%E4%BB%B6%E6%96%B9%E6%B3%95%20-%20Android%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0_%E6%89%8B%E6%9C%BA%E5%BC%80%E5%8F%91%20-%20%E7%BA%A2%E9%BB%91%E8%81%94%E7%9B%9F&ari=2&dbv=2&drs=1&pcs=1153x587&pss=1153x340&cfv=0&cpl=5&chi=1&cce=true&cec=GBK&tlm=1473529717&rw=587<u=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html<r=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html&ecd=1&psr=1280x800&par=1280x705&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1475034422&qn=b34d350ff5a95cdb&tt=1475034419816.1906.2300.2301" width="650" height="180" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
/**
* 根据坐标获取相对应的子控件<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
;
}
</view>