在android中出现可选择的文本菜单后处理textview之外的触摸

编程入门 行业动态 更新时间:2024-10-10 21:32:55
本文介绍了在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:关键词]

本文发布于:2023-04-30 19:12:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选择   后处理   菜单   文本   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!