我使用 TextInputLayout,如果输入字段是必需的,我想以编程方式设置提示文本颜色和浮动标签颜色。在移动到 TextInputLayout 之前,我曾经使用以下代码以编程方式设置提示文本颜色
textField.setHintTextColor(Color.RED);
有人可以指导我如何以编程方式为 TextInputLayout 设置提示文本颜色和浮动标签颜色。
在随附的屏幕截图中,我希望提示文本地址 1在未聚焦时为红色,而在焦点上浮动标签地址 1应为红色。
我使用 TextInputLayout,如果输入字段是必需的,我想以编程方式设置提示文本颜色和浮动标签颜色。在移动到 TextInputLayout 之前,我曾经使用以下代码以编程方式设置提示文本颜色
textField.setHintTextColor(Color.RED);
有人可以指导我如何以编程方式为 TextInputLayout 设置提示文本颜色和浮动标签颜色。
在随附的屏幕截图中,我希望提示文本地址 1在未聚焦时为红色,而在焦点上浮动标签地址 1应为红色。
我用反射改变了聚焦的颜色。这是它可能对某人有帮助的片段。
private void setUpperHintColor(int color) {
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
color
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
编辑 2018-08-01:
如果您使用的是设计库 v28.0.0 及更高版本,则字段已从mDefaultTextColor
todefaultHintTextColor
和 from mFocusedTextColor
to更改focusedTextColor
。
检查其他字段的反编译类。
使用材料组件库,您可以使用:
在布局中:
<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="@color/mycolor"
android:textColorHint="@color/text_input_hint_selector"
.../>
风格:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<!-- The color of the label when it is collapsed and the text field is active -->
<item name="hintTextColor">?attr/colorPrimary</item>
<!-- The color of the label in all other text field states (such as resting and disabled) -->
<item name="android:textColorHint">@color/mtrl_indicator_text_color</item>
</style>
在代码中:
// Sets the text color used by the hint in both the collapsed and expanded states
textInputLayout.setDefaultHintTextColor(...);
//Sets the collapsed hint text color
textInputLayout.setHintTextColor(....);
请好好看看这里的文档:TextInputLayout Methods
有一个方法:
setHintTextAppearance(int resId)
它需要一个可能是样式资源的资源 id!
我会试试这个,看看效果如何!
我希望它对你有帮助!
通常 TextInputLayout 提示文本颜色来自应用程序的 colorAccent。
但是,如果您想更改,则可以使用样式。
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
</android.support.design.widget.TextInputLayout>
@风格
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/Color Name</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/Color Name</item>
<item name="colorControlNormal">@color/Color Name</item>
<item name="colorControlActivated">@color/Color Name</item>
</style>
但是,如果您想添加红色,那么如何区分错误颜色意味着基本标准错误具有红色。
textField.setHintTextColor(Color.RED); 有人可以指导我如何以编程方式为 TextInputLayout 设置提示文本颜色和浮动标签颜色。
setHintTextColor 适用于 API 23+
用于同时更改Focused Color
和Default Text Color
用于 TextInput 布局
private void setInputTextLayoutColor(int color, TextInputLayout textInputLayout) {
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
color
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
编辑:更改 AppCompactEditText 线条颜色
您需要将backgroundTintList
( 或supportBackgroundTintList
)设置为仅包含您希望将色调更改为的颜色EditText
的实例。ColorStateList
以向后兼容的方式执行此操作的简单方法如下所示:
ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)
这将给出EditText
所需的下划线颜色。
让我分享一下我在这方面的经验。我还尝试了在每个相关问题中给出的所有解决方案。即改变子部件的提示颜色为TextInputLayout
我很高兴能稍微详细地分享这个问题的答案。
我们只需要知道:-
将下一行添加到其中一个TextInputLayout
或其子小部件的样式中并没有太大帮助。
<item name="android:textColorHint">@color/white</item>
因为每当焦点被接收/授予可编辑小部件时,它将使用 colorAccent。
这个问题的实际答案是在应用程序的样式标记中添加该样式行,当该或任何可编辑区域不在焦点时,它将设置提示颜色。(这是我们每次都错过的重点)。
如果我们对此有其他信息,请告诉我。
谢谢!
I was facing the quite same problem but with mutable hints and colored symbol. I'm made this trick with data binding and Spannable
in this way.
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/ti_last_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_last_name"
style="@style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onFocusChangeListener="@{viewModel.onFocusChangeListener(@string/last_name)}"
tools:hint="@string/last_name" />
</com.google.android.material.textfield.TextInputLayout>
viewModel.onFocusChangeListener
defined in this way
fun onFocusChangeListener(hint: String) =
OnFocusChangeListener { view, isFocused ->
(view.parent.parent as TextInputLayout).apply {
val editText = (view as TextInputEditText)
if (isFocused) {
this.hintTextColor = ColorStateList.valueOf(this.getColor(R.color.black))
editText.hint = ""
this.hint = hint
} else {
if (!editText.text.isNullOrBlank()) {
this.defaultHintTextColor = ColorStateList.valueOf(this.getColor(R.color.black))
editText.hint = ""
this.hint = hint
} else {
this.hintTextColor = ColorStateList.valueOf(this.getColor(R.color.hint_color))
val builder = SpannableStringBuilder()
builder.append(hint)
val start = builder.length
val end = start + 1
builder.append("\u2981")
builder.setSpan(
ForegroundColorSpan(Color.RED),
start,
end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
builder.setSpan(
SuperscriptSpan(),
start,
end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
editText.hint = builder
this.hint = ""
}
}
}
}
It's allow to use different hints and colors for focused/not focused states and has colored span.
this.getColor()
it's just extension
fun View.getColor(color: Int): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.context.resources.getColor(color, context.theme)
} else {
this.context.resources.getColor(color)
}