如何阻止EditText专注于Android中的Activity启动 [英] How to stop EditText from gaining focus at Activity startup in Android
问题描述
我在Android中有一个Activity
,其中包含两个元素:
I have an Activity
in Android, with two elements:
-
EditText
-
ListView
EditText
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);
没有运气.我如何说服EditText
在Activity
启动时不选择自身?
No luck. How can I convince the EditText
to not select itself when the Activity
starts?
推荐答案
Luc和Mark给出了很好的答案,但是缺少一个好的代码示例.像以下示例一样,将标记android:focusableInTouchMode="true"
和android:focusable="true"
添加到父版式(例如LinearLayout
或ConstraintLayout
)将解决此问题.
Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"
and android:focusable="true"
to parent layout (e.g. LinearLayout
or ConstraintLayout
) like 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屋!