我使用反射改变了聚焦颜色。以下是代码片段,希望对某些人有所帮助。
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
更改为defaultHintTextColor
,并且从mFocusedTextColor
更改为focusedTextColor
。
检查反编译类以获取其他字段。
mFocusedTextColor
更改为 focusedTextColor
mDefaultTextColor
更改为 defaultHintTextColor
mCollapsingTextHelper
更改为 collapsingTextHelper
mCollapsedTypeface
更改为 collapsedTypeface
使用Material Components库,您可以使用以下功能:
在布局中:
<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(....);
setHintTextAppearance(int resId)
这里需要一个资源ID,可以是样式资源!
我会尝试一下,看看效果如何!
希望能对你有所帮助!
<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
<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及以上版本。
要更改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的线条颜色
您需要在EditText
上设置backgroundTintList
(或supportBackgroundTintList
)为一个ColorStateList
实例,该实例只包含您希望更改着色的颜色。以下是一种向后兼容的简单方法:
ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)
这将为EditText
提供所需的下划线颜色。
mDefaultTextColor
。如果我们决定更改浮动文本颜色,即使EditText被禁用,也是必需的。 - Cheok Yan Cheng我曾经遇到过类似的问题,但是涉及可变提示和彩色符号。我通过数据绑定和Spannable
技巧解决了这个问题。
<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
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 = ""
}
}
}
}
允许为聚焦/非聚焦状态使用不同的提示和颜色,并具有带颜色的跨度。
this.getColor()
只是一个扩展
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)
}
让我分享一下我的经验。我也尝试了所有与此相关的问题中提供的解决方案,即更改子小部件的提示颜色为TextInputLayout
我很高兴能够详细地回答这个问题。
我们需要知道的是:
将以下行添加到TextInputLayout
或其子小部件的样式中并没有多大帮助。
<item name="android:textColorHint">@color/white</item>
因为每当焦点接收/授予可编辑小部件时,它都会使用colorAccent。
这个问题的真正答案是在应用程序的样式标签中添加该样式行,这样当那个或任何可编辑区域不处于焦点状态时,它将设置提示颜色。(这是我们每次都错过的关键点)。
如果我们有其他信息,请告诉我。
谢谢!
OnFocusChangeListener
**。我不知道它是否有效。 - wngxaoInputTextLayout.getEditText().setHintTextColor(Color.RED);
- Stanojkovic