可以使用以下代码更改编辑文本的焦点提示条颜色:
EditText editText = findViewById(R.id.editText);
int focusedColor = ContextCompat.getColor(this, R.color.colorFocusedHint);
int unfocusedColor = ContextCompat.getColor(this, R.color.colorUnfocusedHint);
ColorStateList colorStateList = new ColorStateList(
new int[][]{new int[]{android.R.attr.state_focused}, new int[]{}},
new int[]{focusedColor, unfocusedColor});
editText.setHintTextColor(colorStateList);
其中,colorFocusedHint
和colorUnfocusedHint
是定义在colors.xml
文件中的颜色值,分别表示输入框有焦点和没有焦点时的颜色。使用ColorStateList
对象将这两种颜色组合起来,并使用setHintTextColor()
方法将其应用到输入框中。
上一篇:编辑文本的光标在出错时变为黑色