android NestedScrollView嵌套EditText 焦点冲突处理

处理思路: 监听NestedScrollView的滑动状态 当EditText 滑动到屏幕不可见时 让EditText 移除焦点

代码如下

nestedScrollView.setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
                    override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                        val p = Point()
                        windowManager.defaultDisplay.getSize(p)
                        val screenWidth = p.x
                        val screenHeight = p.y
                        val rect = Rect(0, 0, screenWidth, screenHeight)
                        val location = IntArray(2)
                        editText.getLocationInWindow(location)
                        if (editText.getLocalVisibleRect(rect)){
                            // 控件在屏幕可见区域
                        }else{
                            // 控件已不在屏幕可见区域,清除焦点
                            editText.clearFocus()
                        }
                    }
                })

你可能感兴趣的:(android NestedScrollView嵌套EditText 焦点冲突处理)