【发布时间】:2020-06-05 22:35:26
【问题描述】:
就像那里的数百个问题一样,我想更改提示的颜色。最初我想在 cmets 中询问另一个此类问题(未回答),但我没有足够的声誉,所以我在这里问。
我希望 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>
有没有办法为所有视图以编程方式扩展和更改它,或者设置一个全局监听器或使用样式和选择器的正确方法。
【问题讨论】:
-
设置 TextInputLayout Theme ="MyHintStyle" 并删除 app:hintTextAppearance="@style/MyHintStyle" 后尝试。
-
还是一样。如果焦点颜色为深蓝色,则默认为灰色
-
您必须使用
hintTextColor和android:textColorHint属性。检查这个answer -
只有在聚焦时才会改变提示
标签: android android-textinputlayout android-textinputedittext