本文介绍了在android中出现可选择的文本菜单后处理textview之外的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我已经通过
在android中实现了可选择的文本android:textIsSelectable="true"
我现在需要做的是在文本/菜单/光标以外的任何地方触摸时使菜单消失.我如何实现这一目标?
What I now need to do is make the menu disappear when there is touch anywhere other than the text/menu/cursors. How do I achieve this?
推荐答案
首先,您可以点击此链接隐藏软件键盘/单击外部编辑文本(或您想要实现的任何视图类型)后执行操作处理外部点击
Firstly you can follow this link to hide software keyboard/do action after click outside edittext(or any view type you want to achieve Handle outside click
例如
fun setupUI(view: View, action: () -> Unit) {
// Set up touch listener for non-text box views to hide keyboard.
if (view !is EditText && view !is AppCompatEditText) {
view.setOnTouchListener { _, _ ->
action.invoke()
false
}
}
//If a layout container, iterate over children and seed recursion.
if (view is ViewGroup) {
for (i in 0 until view.childCount) {
val innerView: View = view.getChildAt(i)
setupUI(innerView, action)
}
}
}
然后在您的操作中清除您的 rootview 焦点以关闭文本复制菜单:rootView.clearFocus()
Then in your action clear your rootview focus to dismiss text copy menu: rootView.clearFocus()
这篇关于在android中出现可选择的文本菜单后处理textview之外的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论