不关注此内容时显示TextInputEditText的标签

问题描述

我有一个带有两个TextInputEditText的布局,当我在上面设置一个文本时(以编程方式或使用xml),然后将焦点放在另一个TextInputEditText上,上面的标签{{1 }}已隐藏。 我想显示这个标签,即使在其中写了文字

我的TextInputEditText代码

xml

我的风格:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/whiteGray2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:orientation="horizontal"
    android:weightSum="2"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp">
    <com.google.android.material.textfield.TextInputLayout
        style="@style/CustomBorderTextInputEditText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:hint="@string/oldCount"
        app:hintTextColor="@color/gray_text">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/input_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:inputType="none"
            android:imeOptions="actionNext"
            android:text="Hi How are you"
            android:textColor="@color/gray_text"
            android:textColorHint="@color/gray_text" />

    </com.google.android.material.textfield.TextInputLayout>
    <com.google.android.material.textfield.TextInputLayout
        style="@style/CustomBorderTextInputEditText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:hint="@string/newCount">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/newCount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textColorHint="@color/gray_text"
            android:hint="@string/newCount"
            android:textColor="@color/gray_text"
            android:inputType="number"/>

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

I n

解决方法

将其添加到您的xml代码中:

android:focusableInTouchMode="true"

将其添加到您的Java代码中:

 EditText newCount = (EditText)findViewById(R.id.newCount);
 newCount.setFocusable(true);

可能对您有用,请尝试一下。

,

我找到了答案。我应该添加textColorHint并设置颜色,而不是添加hintTextColor属性。但是现在我很困惑,两者之间有什么区别?

,

您的问题在这里:

    <com.google.android.material.textfield.TextInputEditText
        android:textColorHint="@color/gray_text"

删除android:textColorHint中的TextInputEditText并使用:

<com.google.android.material.textfield.TextInputLayout
      app:hintTextColor="@color/colorSecondary"
      android:textColorHint="@color/text_input_hint_selector"

hintTextColor 是折叠标签且文本字段处于活动状态时标签的颜色。

android:textColorHint 是所有其他文本字段状态(例如,静止和禁用)中标签的颜色。

只是一个例子:

enter image description here

这是选择器:
enter image description here

TextInputLayout

无文字:
enter image description here

重点:
enter image description here

文字不集中:
enter image description here

已禁用:

enter image description here