如何阻止EditText专注于Android中的Activity启动 [英] How to stop EditText from gaining focus at Activity startup in Android

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

问题描述

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

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView
  1. EditText
  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 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屋!

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