【问题标题】:Show label of TextInputEditText when not focused on this不关注此内容时显示 TextInputEditText 的标签
【发布时间】:2020-09-02 04:07:09
【问题描述】:

我有一个带有两个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>

【问题讨论】:

    标签: android user-interface material-components-android android-textinputlayout android-textinputedittext


    【解决方案1】:

    您的问题在这里:

        <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

    无文字:

    关注:

    文本未聚焦:

    已禁用:

    【讨论】:

      【解决方案2】:

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

      android:focusableInTouchMode="true"
      

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

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

      可能对你有用,试试吧。

      【讨论】:

      • 感谢您的回复。我现在已经检查过了,但它没有用
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 2018-03-09
        • 1970-01-01
        • 2011-09-28
        相关资源
        最近更新 更多