TextInputLayout浮动时更改提示颜色

问题描述 投票:0回答:1

就像那里的数百个问题一样,我想更改提示的颜色。本来我想在另一个这样的问题的评论中提问(未回答),但是我没有足够的声誉,所以我在这里提问。

我希望当提示在TextInputEditText中为一种颜色时,而当其浮动为另一种颜色时。在其他帖子中,答案始终是聚焦视图时如何更改提示。那不是我想要的,我可以做到。如果“编辑文本”不为空,我想更改提示颜色。我正在使用材质设计textInputLayout和textInputEditText。尝试了不同的样式和选择器,但仍然无法做到。试图扩展TextInputLayout,但我不知道如何将其绑定到EditText,以便可以将OnTextListener用于子视图。

到目前为止,我的代码仅在焦点对准视图时才能更改提示的颜色。

样式:

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>
    <item name="hintTextColor">@color/dark_blue_60</item>
</style>

<style name="MyHintStyle" parent="@android:style/TextAppearance">
    <!-- Hint color when floating and focused-->
    <item name="android:textColor">@color/dark_blue_60</item>
    <!-- doesn't change anything -->
    <item name="android:textColorHint">@color/dark_blue</item>
    <item name="colorAccent">@color/green_03</item>

    <item name="colorControlNormal">@color/green_03</item>
    <item name="colorControlActivated">@color/green_03</item>

</style>

XML:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:textColorHint="@color/dark_blue"
            android:theme="@style/TextInputLayoutAppearance"
            app:errorEnabled="true"
            app:hintTextAppearance="@style/MyHintStyle">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:backgroundTint="@color/green_03"
                android:hint="@string/phoneNumber"
                android:textColor="@color/dark_blue"
                android:inputType="number"
                android:maxLength="3"
                android:text="12" />

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

是否可以通过编程方式为所有视图扩展和更改它,或者设置全局侦听器,还是使用样式和选择器的正确方法。

android android-textinputlayout android-textinputedittext
1个回答
0
投票

我认为您可以检查editext是否包含某些内容或是否被重点关注。如果是这样,那么您将更改提示的颜色。最好的检查方法-EditText的View.FocusListener。

© www.soinside.com 2019 - 2024. All rights reserved.