以编程方式将 TextInputLayout textColorHint 颜色更改为 indicate/show 内部禁用的 editText

programmatically change TextInputLayout textColorHint color to indicate/show a disabled editText inside

我在 TextInputLayout 中有一个 EditText。我想以编程方式更改 TextInputLayout 的 textColorHint 的颜色。因为我需要在用户填写表格时启用和禁用 editTexts。例如,带有 5 个编辑文本和 3 个按钮的忘记密码表单。用户必须提交他们的电子邮件、验证码、新密码和确认密码。只有当他们的邮箱提交后,验证码EditText,验证提交按钮才会启用。等等。

因此,为了显示禁用,我希望 EditText xml 中的 EditText 文本(占位符)也称为 android:hint,下划线稍微变灰/更透明。可以使用 setAlpha(.5f) 解决此问题;但我想改变颜色。将我的 textPlaceholderColor(grey) 更改为浅灰色。

我尝试了app:hintTextApperance=@style/TextInputLayoutHintText“方法”,但android:TextColorHint似乎在里面不起作用。我什至尝试使用 codeLayout.setHintTextAppearance(R.style.TextInputLayoutHintTextDisabled); 更改样式我还在 EditText 中尝试了选择器“方法”。但是 android:TextColorHint 似乎在 EditText 中不起作用;为其设置颜色不起作用。它必须在 TextInputLayout 中进行更改。

在我的样式文件中

    <!-- Legacy TextField Theme-->
    <style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
        <item name="colorControlNormal">@color/textPlaceholderColor </item>
        <item name="colorControlActivated">@color/textFieldColor</item>
        <item name="colorControlHighlight">@color/textFieldColor</item>
    </style>


    <!-- TextField Hint Theme-->
    <style name="TextInputLayoutHintText">
        <item name="android:textColor">@color/textPlaceholderColor</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColorHint">@color/disableTextColor </item>
    </style>

    <!-- TextField Text Color Hint Theme Disabled-->
    <style name="TextInputLayoutHintTextDisabled">
        <item name="android:textColor">@color/disableTextColor </item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColorHint">@color/disableTextColor </item>
        <item name="hintTextColor">@color/disableTextColor </item>
    </style>

在我的 activity xml 文件中

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/forgetTextInputLayout2"
            style="@style/TextInputLayoutAppearance"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="20dp"
            android:elevation="12dp"
            app:endIconMode="clear_text"
            android:textColorHint="@color/textPlaceholderColor"
            app:hintTextAppearance="@style/TextInputLayoutHintText"
            app:layout_constraintEnd_toEndOf="@+id/forgetForm"
            app:layout_constraintStart_toStartOf="@+id/forgetForm"
            app:layout_constraintTop_toBottomOf="@+id/forget_submit_email">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/forgetVerification"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:fontFamily="@font/open_sans"
                android:hint="@string/ForgetForm3"
                android:inputType="textPersonName"
                android:singleLine="true"
                android:textColor="@drawable/edit_text_selector"
                android:textCursorDrawable="@drawable/cursor_color"
                android:textSize="18sp" />
        </com.google.android.material.textfield.TextInputLayout>

彩色文件

  <color name="colorPrimary">#00BEBA</color>
    <color name="colorPrimaryDark">#00BEBA</color>
    <color name="colorAccent">#F60606</color>
    <color name="white">#FFFFFF</color>
    <color name="green">#17B3AB</color>
    <color name="version">#0B0008</color>
    <color name="appBackgroundColor">#00BEBA</color>
    <color name="textPlaceholderColor">#807E7E</color>
    <color name="textFieldColor">#000000</color>
    <color name="textFieldCursorColor">#FB0000</color>
    <color name="textFieldHightLightColor">#2196F3</color>
    <color name="customGreen">#92CFCD</color>
    <color name="defaultLinkBlue">#007aff</color>
    <color name="facebookBlue">#3B5998</color>
    <color name="disableTextColor">#3C95A1A0</color>

在样式文件中使用此样式:

 <style name="EditTextWrapper">
        <item name="android:layout_width">match_parent</item>
        <item name="android:textAppearance">@style/TextAppearance_open_sans_semibold</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColorHint">@color/brown</item>
        <item name="counterOverflowTextAppearance">@style/CounterOverFlow</item>
        <item name="counterTextAppearance">@style/CounterText</item>
        <item name="errorTextAppearance">@style/ErrorText</item>
        <item name="hintTextAppearance">@style/CustomHint</item>
    </style>

    <style name="CounterOverFlow" parent="TextAppearance.Design.Counter.Overflow">
        <item name="android:textSize">1sp</item>
        <item name="android:textAppearance">@style/TextAppearance_open_sans_semibold</item>
        <item name="android:textColor">@color/green</item>
    </style>

    <style name="CustomHint" parent="TextAppearance.Design.Hint">
        <item name="android:textSize">@dimen/sp11</item>
        <item name="android:textColor">@color/green</item>
        <item name="colorAccent">@color/green</item>
        <item name="colorControlNormal">@color/green</item>
        <item name="android:textAppearance">@style/TextAppearance_open_sans_semibold</item>
        <item name="colorControlActivated">@color/green</item>
        <item name="android:textColorHint">@color/green</item>
        <!--<item name="android:textAppearance">@style/TextAppearance.angelina_script</item>-->
    </style>

    <style name="CounterText" parent="TextAppearance.Design.Counter">
        <item name="android:textSize">1sp</item>
        <item name="android:textAppearance">@style/TextAppearance_open_sans_semibold</item>

        <item name="android:textColor">@color/green</item>
    </style>

    <style name="ErrorText" parent="TextAppearance.Design.Error">
        <item name="android:textSize">@dimen/sp12</item>
        <item name="android:textColor">@color/red</item>
        <item name="android:textAppearance">@style/TextAppearance_open_sans_semibold</item>

        <item name="android:textColorHint">@color/red</item>
    </style>

布局中的使用方式:

  <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/wrapper_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColorHint="@color/white"
                    android:theme="@style/EditTextWrapper">

                <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/edt_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:drawableRight="@drawable/ic_lock"
                        android:drawablePadding="@dimen/dp16"
                        android:hint="@string/password"
                        android:textAppearance="@style/TextView_Regular"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:textColor="@android:color/white"
                        android:textSize="16sp" />
            </com.google.android.material.textfield.TextInputLayout>

据我了解你的问题你想确保所有编辑文本字段都正确填写。为此你可以使用 textwatcher。将 textwatcher 添加到您的 edittext 字段并检查条件并在条件为真或假时启用或禁用按钮。

这是一个示例代码

    //Say you have three edit fields and a button

EditText name = findviewbyId(R.id.name);
EditText email = findviewbyId(R.id.email);
EditText mobile = findviewbyId(R.id.mobile);

name.addTextWatcherListener(checkEmpty);
email.addTextWatcherListener(checkEmpty);
mobile.addTextWatcherListener(checkEmpty);


//Now add the method 

public TextWatcher checkEmpty = new TextWatcher(){

/* Here you'll get three method use the onTextChanged method and get the strings from edit text fields and check if they're empty or not if empty disable button else enable */ 
}

您可以使用自定义样式:

    <com.google.android.material.textfield.TextInputLayout
        style="@style/MyTextInputLayout"

定义一个选择器:

  <style name="MyTextInputLayout " parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >    
   <item name="android:textColorHint">@color/my_selector</item>.  
  </style>

与:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.38" android:color="@color/colorSecondaryLight" android:state_enabled="false"/>
    <item android:alpha="0.6" android:color="@color/colorPrimary"/>
</selector>

这样enabling/disablingTextInputLayout颜色就变了

您可以通过编程方式使用 setDefaultHintTextColor 方法:

    TextInputLayout textInputLayout = findViewById(R.id.til);                   
    textInputLayout.setDefaultHintTextColor
      (ContextCompat.getColorStateList(this,R.color.selector));