Показывать метку TextInputEditText, когда он не сфокусирован на этом

У меня есть макет с двумя TextInputEditText, и когда я устанавливаю на нем текст (программно или с помощью xml) и устанавливаю фокус на другом TextInputEditText, вышеуказанная метка 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>

Мой стиль:

 <style name="CustomBorderTextInputEditText" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
    <item name="hintTextColor">?attr/colorPrimary</item>
</style>

И н




Ответы (3)


Ваша проблема здесь:

    <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 — это цвет метки во всех других состояниях текстового поля (например, в состоянии покоя и отключен).

Просто пример:

введите здесь описание изображения

А это селектор:
введите здесь описание изображения

Вот TextInputLayout:

Без текста:
введите здесь описание изображения

Ориентировано:
введите здесь описание изображения

Текст не сфокусирован:
введите здесь описание изображения

Неполноценный:

введите здесь описание изображения

person Gabriele Mariotti    schedule 02.09.2020

Добавьте его в свой XML-код:

android:focusableInTouchMode="true"

or

Добавьте его в код Java:

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

может быть это полезно для вас, просто попробуйте.

person Sree Durkesh    schedule 02.09.2020
comment
Спасибо за ваш ответ. Я проверил это сейчас, но это не сработало - person Fahim; 02.09.2020

Я нашел свой ответ. Я должен добавить textColorHint и установить цвет вместо добавления атрибута hintTextColor. Но теперь я в замешательстве, в чем разница между ними обоими?

person Fahim    schedule 02.09.2020