Android - 如何判断用户是否“点击” TextView 之外的任何地方?

发布于 01-03 21:02 字数 226 浏览 4 评论 0原文

我有一个 TextView ,可以在触摸时转换为 EditView 。但是,我希望当用户触摸 EditView 之外的任何位置时发生相反的情况。我尝试过使用 setOnFocusChangeListener,但这似乎只有在其他东西获得焦点时才起作用。我知道我可以拦截 onTouch 并检查那里,但是有没有更简单的方法来处理这种情况?

I have a TextView that converts to an EditView on touch. However, I want the opposite to occur when a user touches anywhere outside of the EditView. I've tried to use setOnFocusChangeListener, but that only appears to work when something else gains focus. I know I can intercept onTouch and check there, but is there a simpler way to handle this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟2025-01-10 21:02:37

我也在我的项目中使用了这个,并解决了这个问题,如下所示:

我将 EditText 外部的布局设置为“focusable=true”和“clickable=true”,EditText 没有背景
所以它看起来像一个 TextView,焦点更改时我更改 EditText 的文本颜色,以便用户
可以看到他/她可以编辑文本。然后,布局外面有一个OnClickListener,点击时
我隐藏软件键盘并将文本颜色更改回 TextView 颜色。这一切
无需切换元件即可工作。如果需要,您可以将这两个元素放入
FrameLayout 确保元素在布局中具有相同的位置,实现
TextView 上的 OnClickListener,然后在 onClick 事件中切换对象可见性。

也许这个想法对你有帮助;)

i have used this in my project too and have solved this as follows:

i set the layout outside from EditText as "focusable=true" and "clickable=true", the EditText have no background
so it looks like a TextView, on focus change i change the text color of EditText so the user
can see he/she can edit the text. then, the layout outside have a OnClickListener, on click
i hide the software keyboard and change the text color back to the TextView color. This all
is working without to switch elements. If you want, you can place both the elements in
a FrameLayout to make sure the elements have the same positions in the layout, implement a
OnClickListener on the TextView, in the onClick event you then switch the Objects visibility.

maybe this idea helps you ;)

疑心病2025-01-10 21:02:37

设置 TextView 的父级并将编辑视图设置为可聚焦。

使所有布局和视图可点击,并在每个布局和视图上实现 onClickListener,如下编写 onClickListener :

private ClickListener implements OnClickListener
{
    public void onClick(View view)
    {
         if(v.getId()!=R.id.editView)
         {
              //Write your code here to replace views
         }
    }
}

Set parent of TextView and edit view to focusable.

Make all layouts and views clickable, and implement onClickListener on each, write onClickListener like below:

private ClickListener implements OnClickListener
{
    public void onClick(View view)
    {
         if(v.getId()!=R.id.editView)
         {
              //Write your code here to replace views
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文