S.A*_*ley 17 android android-textinputlayout
我使用TextInputLayout,如果输入字段是必需的,我想以编程方式设置提示文本颜色和浮动标签颜色.在移动到TextInputLayout之前,我曾使用以下方法以编程方式设置提示文本颜色
textField.setHintTextColor(Color.RED);
有人可以指导我如何以编程方式为TextInputLayout设置提示文本颜色和浮动标签颜色.
在附带的屏幕截图中,我希望提示文本地址1在未聚焦时为红色,并且浮动标签地址1的焦点应为红色.
ale*_*ius 18
我用反射改变了聚焦颜色.这是它可以帮助某人的片段.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2018-08-01:
如果您使用的是设计库v28.0.0及更高版本,则字段已从更改mDefaultTextColor为defaultHintTextColor和从更改mFocusedTextColor为focusedTextColor.
检查反编译类以查找其他字段.
Gab*_*tti 14
使用Material Components 库,您可以使用:
在布局中:
<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="@color/mycolor"
android:textColorHint="@color/text_input_hint_selector"
.../>
Run Code Online (Sandbox Code Playgroud)
风格:
<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>
Run Code Online (Sandbox Code Playgroud)
在代码中:
// 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(....);
Run Code Online (Sandbox Code Playgroud)
请在这里仔细阅读文档:TextInputLayout方法
有一种方法:
setHintTextAppearance(int resId)
Run Code Online (Sandbox Code Playgroud)
哪个资源ID可能是样式资源!
我会尝试一下,看看效果如何!
希望对您有帮助!
| 归档时间: |
|
| 查看次数: |
17136 次 |
| 最近记录: |