简体   繁体   English

如何在 Android 上使用 x 和 y position 获取视图

[英]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.

相关问题 在Android中放大后如何获取视图的xy位置 - How to get the x y position of a view after zoom-in in android Android:如何在滚动视图中获取视图的 Y 位置? - Android: how to get the Y position of a view in a scrollView? 如何在android中获取视图的x,y坐标? - How to get the x,y coordinates of a view in android? 如何在onCreate上获取android中图像的x和y位置? - How can get x and y position of an image in android on onCreate? 如何在Android中的画布上获取圆形drwan的位置(x和y坐标)? - how to get position(x and y coordinates) of circle drwan on canvas in android? Java Android:如何在 GLSurfaceView 的位置(X,Y)处获取像素颜色? - Java Android: how to get pixel color at position (X, Y) of GLSurfaceView? 如何在EditText android中获取光标位置(x,y) - How to get cursor position (x,y) in EditText android android - 如何在imageview中获取图像边缘x / y位置 - android - how to get the image edge x/y position inside imageview 如何在android TextView中获取点击跨度的x,y位置? - How to get x,y position of clicked span in android TextView? Android onAccessibilityEvent获取TextView的X和Y位置 - Android onAccessibilityEvent get X and Y position of TextView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM