我正在对动态TextInputLayout进行充气,并且想要更改texthintColor并使用以下内容,但最终抛出异常
private fun setInputTextLayoutColor(color: Int, textInputLayout: TextInputLayout) {
try {
val field = textInputLayout.javaClass.getDeclaredField("mFocusedTextColor")
field.isAccessible = true
val states = arrayOf(intArrayOf())
val colors = intArrayOf(color)
val myList = ColorStateList(states, colors)
field.set(textInputLayout, myList)
val fDefaultTextColor = TextInputLayout::class.java.getDeclaredField("mDefaultTextColor")
fDefaultTextColor.isAccessible = true
fDefaultTextColor.set(textInputLayout, myList)
val method =
textInputLayout.javaClass.getDeclaredMethod("updateLabelState", Boolean::class.javaPrimitiveType)
method.isAccessible = true
method.invoke(textInputLayout, true)
} catch (e: Exception) {
e.printStackTrace()
}
}
setInputTextLayoutColor(resources.getColor(R.color.colorPrimary),myTextInputLayout)
支持库版本
implementation 'com.android.support:design:28.0.0'
printStackTrace异常:
java.lang.NoSuchFieldException: mFocusedTextColor