[英]How to get view with x and y position on Android
I am trying to get view in dispatchTouchEvent()
.我正在尝试在dispatchTouchEvent()
中查看。
However, this only passes MotionEvent.但是,这只会传递 MotionEvent。 It has x and y position.它有 x 和 y position。 But, it doesn't pass view itself.但是,它不会通过视图本身。
I can get view with currentFocus
.我可以用currentFocus
查看。 However, this gets view after having a focus.但是,这在获得焦点后会得到视图。 And when you touch EditText and then Outside of EdiText, EditText still has the focus.当您触摸 EditText 然后在 EdiText 之外触摸时,EditText 仍然具有焦点。 So, currentFocus
will be the same EditText.因此, currentFocus
将是相同的 EditText。
What I am trying to do is closing keyboard when I touch non-EditText.我想要做的是在我触摸非 EditText 时关闭键盘。 And showing keybaord when I touch EditText.并在我触摸 EditText 时显示键盘。 But when I touch EditText A and then EditText B, it shouldn't be blinking(hiding and showing again)但是当我触摸 EditText A 和 EditText B 时,它不应该闪烁(隐藏并再次显示)
What I was trying is:我正在尝试的是:
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
var view = currentFocus
if (view == null) {
view = View(this@BaseActivity)
}
ev?.let {
val isInRange = isRegionOfInterest(view, ev)
if(!isInRange){
hideKeyboard()
currentFocus?.clearFocus()
}
}
return super.dispatchTouchEvent(ev)
}
But this doesn't seem to be the right way.但这似乎不是正确的方法。 It'd be great if I can get a view with X and Y positon.如果我能看到 X 和 Y 位置的视图,那就太好了。 Do you know any way to do that?你知道有什么办法吗?
This is what I did:这就是我所做的:
private fun isHidableArea(x: Float?, y: Float?): Boolean {
var result = true
if (x == null || y == null) return result
val rootView = (findViewById<View>(R.id.content) as ViewGroup).getChildAt(0) as ViewGroup
val interestViewList = rootView.findInterestViewListByPosition(x, y)
loop@ for (v in interestViewList) {
if (v is EditText) {
result = false
break@loop
}
}
return result
}
private fun View.findInterestViewListByPosition(x: Float, y: Float): ArrayList<View> {
val childViewList = this.getAllChildren()
val interestViewList = ArrayList<View>()
for (it in childViewList) {
if (isRegionOfInterest(it, x, y)) {
interestViewList.add(it)
}
}
return interestViewList
}
private fun View.getAllChildren(): List<View> {
val result = ArrayList<View>()
if (this !is ViewGroup) {
result.add(this)
} else {
for (index in 0 until this.childCount) {
val child = this.getChildAt(index)
result.addAll(child.getAllChildren())
}
}
return result
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
if (isHidableArea(ev.x, ev.y)) {
hideKeyboard()
}
return super.dispatchTouchEvent(ev)
}
private fun isRegionOfInterest(view: View, x: Float, y: Float): Boolean {
return x in view.x - 0..(view.x + view.width) && y in view.y - 0..view.y + view.height
}
This gets all the child view and compare the position.这将获取所有子视图并比较 position。
EditText, clear focus on touch outside EditText,清晰专注于触摸外
You can try with these solutions.您可以尝试使用这些解决方案。
One uses a FrameLayout as an "interceptor" of touches, the other instead manage the touch event with "dispatchTouchEvent".一个使用 FrameLayout 作为触摸的“拦截器”,另一个使用“dispatchTouchEvent”管理触摸事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.