美文网首页
android NestedScrollView嵌套Edit

android NestedScrollView嵌套Edit

作者: 黑芝麻胡 | 来源:发表于2021-06-24 13:35 被阅读0次

    处理思路: 监听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嵌套Edit

          本文链接:https://www.haomeiwen.com/subject/zeiwyltx.html