android - 在 Android 中启动 Activity 时如何阻止 EditText 获得焦点?

标签 android listview android-activity android-edittext focus

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

  1. 编辑文本
  2. ListView

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

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

运气不好。我怎样才能说服 EditTextActivity 启动时不选择自己?

最佳答案

将标签 android:focusableInTouchMode="true"android:focusable="true" 添加到父布局(例如 LinearLayoutConstraintLayout) 就像下面的例子一样,将解决这个问题。

<!-- 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"/>

关于android - 在 Android 中启动 Activity 时如何阻止 EditText 获得焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6590453/

相关文章:

java - 将 HTTPOnly cookie 与 android 中的其他 cookie 区分开来

android - Fragment 中的 notifyDataSetChanged() 不刷新 ListView

android - 受信任的 Web Activity : Not able to remove URL header

android - FragmentStatePagerAdapter.getItem() 返回错误的位置

Android:tabactivity - 所有选项卡的内容首先重叠

安卓 : Showing keyboard hides listview contents

android - 新 Activity 空指针异常

android - Android 中多个 fragment 之间的通信

android - 同一应用程序中的隐式 Intent

java - 将整数转换为字符串的问题