阻止EditText在Activity启动时启动键盘?

Joh*_*eth 1 android android-edittext

我的一种应用程序布局中有一个“编辑文本”,并且我希望此EditText仅在实际被触摸时才打开键盘(我相信这被称为“专注于?”)。

截至目前,每当应用程序打开时,键盘就会与EditText一起打开,这不是我想要的。

我尝试了许多不同的XML标记来解决此问题:

android:focusable="false" <--- Prevents keyboard from opening at all.

android:focusable="true"
android:focusableInTouchMode = "true"     <--- These tags give me the same result as no tags (keybaord will open on activity start)

android:focusedByDefault = "true" <--- Only available in API >= 23
Run Code Online (Sandbox Code Playgroud)

我要问的是,为什么很难禁用EditText的默认焦点?当然,我缺少执行此操作的简单方法。

编辑:将此行添加到我的AndroidManifest中解决了此问题:android:windowSoftInputMode =“ stateHidden”但是,我不喜欢此解决方案。似乎因为这是在清单中,所以它将影响比我需要更改的单个EditText更多的UI元素。

wyz*_*ard 7

或者,您可以将焦点设置到根布局元素:

android:focusable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <EditText
        android:inputType="text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)