如何在TextInputLayout中禁用即时焦点?

31

我在我的NavigationDrawer中使用了TextInputLayout(+搜索)。当用户切换此抽屉时,光标焦点会立即出现在TextInputLayout字段上。

输入图像描述

是否可以在用户单击它之前不将焦点放在该字段上?

谢谢!


添加以下内容到您的xml中 android:focusable="false" - Ravi Gadipudi
解决方案在这里 https://dev59.com/SpTfa4cB1Zd3GeqPPViN#45009744 - Vinil Chandran
4个回答

61

在您的根视图中使用此属性:

        android:focusableInTouchMode="true"

4
你的根视图是否应该是可聚焦的?可能不应该是 :| - ataulm

6

在我的情况下,这些答案都没有帮助我。我做的是:

editText.post(() -> {
        textInputLayout.setHintAnimationEnabled(false);
        editText.clearFocus();
        textInputLayout.setHintAnimationEnabled(true);
    });

这是从设计支持库v23开始提供的。

它在一些设备上运行正常,但并非所有设备都能运行。 - sohel.eco

2

在onCreate()方法中添加以下代码:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

或者将以下内容添加到您的清单文件中的活动条目中:
android:windowSoftInputMode="stateHidden"

2
如果您在textInputLayout内部设置setText到EditText中,则无法正常工作。 - StuartDTO

0
我解决了这个问题,只需要添加另一个EditText并将其隐藏即可。
        <EditText
          android:layout_width="0dp"
          android:layout_height="0dp" />
        <android.support.design.widget.TextInputLayout
            android:id="@+id/NghiPhep_TextInputLayout_GhiChu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintTextAppearance="@style/TextHintLabel"
            android:layout_marginTop="5dp"
            android:focusableInTouchMode="true"
            >
            <EditText
                android:id="@+id/NghiPhep_txt_GhiChu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimaryDark"
                android:hint="Ghi chú"
                android:fontFamily="@font/arial_rounded"
                android:inputType="textMultiLine"
                android:singleLine="false"
                android:textSize="18sp"/>
        </android.support.design.widget.TextInputLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接