Android 以编程方式设置TextInputLayout提示文本颜色和浮动标签颜色
我使用TextInputLayout,如果输入字段是必需的,我希望通过编程设置提示文本颜色和浮动标签颜色。在转到TextInputLayout之前,我使用以下命令以编程方式设置提示文本颜色Android 以编程方式设置TextInputLayout提示文本颜色和浮动标签颜色,android,android-textinputlayout,Android,Android Textinputlayout,我使用TextInputLayout,如果输入字段是必需的,我希望通过编程设置提示文本颜色和浮动标签颜色。在转到TextInputLayout之前,我使用以下命令以编程方式设置提示文本颜色 textInputLayout.setErrorEnabled(true); textInputLayout.setError(" "); textField.setHintTextColor(Color.RED) 有人可以指导我如何通过编程为TextInputLayout设置提示文本颜色和浮动标签颜色吗
textInputLayout.setErrorEnabled(true);
textInputLayout.setError(" ");
textField.setHintTextColor(Color.RED)代码>
有人可以指导我如何通过编程为TextInputLayout设置提示文本颜色和浮动标签颜色吗
在随附的屏幕截图中,我希望提示文本地址1在未聚焦时为红色,浮动标签地址1上的聚焦应为红色
请仔细阅读以下文档:
有一种方法:
setHintTextAppearance(int resId)
它接受一个可能是样式资源的资源id
我会试试这个,看看效果如何
我希望它能帮助你 我能够使用以下命令获得红色的浮动标签
textInputLayout.setErrorEnabled(true);
textInputLayout.setError(" ");
我通过反射改变了聚焦颜色。下面是一个片段,它可能会帮助一些人
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:
<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(....);
如果您使用的是设计库v28.0.0及更高版本,则字段已从mDefaultTextColor
更改为defaultHintTextColor
,并从mFocusedTextColor
更改为focusedTextColor
检查反编译类中的其他字段。通常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>
@颜色/颜色名称
20便士
@颜色/颜色名称
@颜色/颜色名称
@颜色/颜色名称
但若你们想添加红色,那个么你们如何区分错误颜色意味着基本标准错误有红色
textField.setHintTextColor(Color.RED);有人能指导我怎么做吗
以编程方式设置提示文本颜色和浮动标签颜色
对于TextInputLayout
setHintTextColor适用于API 23+用于更改文本输入布局的聚焦颜色和默认文本颜色
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)
这将为编辑文本提供所需的下划线颜色。让我分享一下我的经验。我还尝试了在每个与此相关的问题中给出的所有解决方案。i、 e.将子窗口小部件的提示颜色更改为TextInputLayout
我很高兴与大家分享这个问题的答案,并提供一些细节
我们需要知道的是:-
在TextInputLayout
或其子小部件的样式中添加以下行并没有多大帮助
@color/white
因为每当焦点被接收/授权给可编辑小部件时,它都将使用colorAccent
这个问题的实际答案是在应用程序的样式标记中添加该样式行,当该样式行或任何可编辑区域不在焦点时,它将通过该样式行设置提示颜色。(这是我们每次都错过的一点)
请让我知道,如果我们有这方面的其他信息
谢谢 使用,您可以使用:
在布局中:
<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(....);
我面临着完全相同的问题,但有着可变的提示和彩色符号。我用数据绑定和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>
它允许对聚焦/非聚焦状态使用不同的提示和颜色,并具有彩色范围
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)
}
如果(isMandatory()){textInputLayout.setHintTextAppearance(Color.RED);}仍然没有成功,请尝试以下操作。isMandatory()是一个指示字段是否为必填字段的方法:-(请尝试这个对我来说不太合适的答案。我尝试了这个textInputLayout.setErrorEnabled(true);textInputLayout.setError(“”);。这将以红色显示浮动标签,但不会显示提示文本。这将作为完整性的答案发布。好的!我很高兴您得到了更接近您所期望的资源类型样式而不是颜色的内容。请确保在发布您自己的描述之前阅读了这些方法。这只是强制TextInputL的一种解决方法布局外观为错误文本。应接受@alexandrius的答案。这解决了我的问题,因此被接受为答案!这不是解决方案。您可以向这些文本字段添加一个OnFocusChangeListener
。我不知道它是否有效。向编辑文本添加一个OnFocusChangeListener,并更改提示颜色,即onFocusChangeListener在其参数中给出了一个布尔值,如果编辑文本具有焦点,则该值为true;如果焦点不在其中,则该值为false。相应地设置hintTextColor。我没有尝试过,但我认为这应该适用于您:InputTextLayout.getEditText().setHintTextColor(Color.RED)@Stanojkovic尝试过这个方法,但它不起作用。你真是个了不起的人。如果你知道我花了多少时间来找到一个解决方案!谢谢,但我真的不建议使用这种方法。我知道你为什么不建议,但我没有找到一种使用Android方法来动态更改颜色的正常方法。你知道怎么做吗?谢谢,我建议你使用creating您自己的输入布局。这就是我做的。请注意,自2019年1月9日以来,已进行了以下更改-MFocustextColor
更改为focusedTextColor
mDefaultTextColor
更改为defaultHintTextColor
mCollapsingTextHelper
更改为collapsingTextHelper
>mCollapsedTypeface
更改为collapsedTypeface
感谢提供