如何阻止 EditText 在 Android 的 Activity 启动时获得焦点 [英] How to stop EditText from gaining focus at Activity startup in Android

查看:30
本文介绍了如何阻止 EditText 在 Android 的 Activity 启动时获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 中有一个 Activity,有两个元素:

I have an Activity in Android, with two elements:

  1. 编辑文本
  2. ListView

当我的 Activity 启动时,EditText 立即具有输入焦点(闪烁的光标).我不希望任何控件在启动时具有输入焦点.我试过了:

When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:

EditText.setSelected(false);
EditText.setFocusable(false);

没有运气.如何说服 EditTextActivity 启动时不要选择自己?

No luck. How can I convince the EditText to not select itself when the Activity starts?

推荐答案

Luc 和 Mark 的出色回答.但是,缺少一个好的代码示例.将标签 android:focusableInTouchMode=true"android:focusable=true" 添加到父布局(例如 LinearLayoutConstraintLayout) 就像下面的例子一样,将解决这个问题.

Excellent answers from Luc and Mark. However, a good code sample is missing. Adding the tags android:focusableInTouchMode="true" and android:focusable="true" to the parent layout (e.g. LinearLayout or ConstraintLayout) like in the following example, will fix the problem.

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

这篇关于如何阻止 EditText 在 Android 的 Activity 启动时获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆