如何移除 EditText 上的输入焦点

发布于 2021-11-20 03:27:10 字数 2402 浏览 742 评论 4

当我们创建一个带 EditText 或 AutoCompleteTextView 的视图时,在加载视图时总是会把输入的焦点自动移动到第一个输入框。如下图所示:

下面是 mail.xml 布局文件:

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
        <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
        </button>
        <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="">
        </edittext>
        <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
        </button>
</linearlayout>

我们如何才能把焦点从 EditText 上移除呢?最简单的方法是创建一个不可见的(invisible)LinearLayout,LinearLayout 将会把焦点从 EditText 上移走。

我们修改 mail.xml 布局文件,在 EditText 之前增加一个 LinearLayout ,如下所示:

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
        <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
        </button>
<linearlayout android:focusable="true" android:focusableintouchmode="true" android:layout_height="0px" android:layout_width="0px">
        <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="">
        </edittext>
        <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
        </button>
</linearlayout>
</linearlayout>

最终效果如下所示:

不需要任何代码就把焦点移除了,是不是最简单的解决方案?

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

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

发布评论

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

评论(4

归属感 2021-11-24 10:00:18

not bad

千笙结 2021-11-24 05:19:49

效果很。。 不管是android:focusable=true 还是false

 结局都是下面的没显示了

终遇你 2021-11-24 04:33:23

old tip

臻嫒无言 2021-11-24 02:26:13

这个达到的效果跟文中的方法有点区别的吧

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文