EditText设置错误提示后光标变为黑色的异常问题排查
OutlinedBox样式下EditText设置错误提示后光标变黑的诡异Bug
我在开发App时遇到一个诡异Bug:将EditText的光标颜色设置为白色(默认是深蓝色),正常状态下显示正常,但调用binding.inputEmailWrapper.error = getString(R.string.email_invalid)设置错误提示后,光标颜色变为黑色。主题中从未将黑色定义为主色调、强调色或表面色,按预期光标应保持原有颜色。
尝试过的操作及结果:
- 将
textCursorDrawable设为null:错误状态下光标仍为白色(与文本颜色一致),但光标厚度消失 - 设置静态Drawable:错误状态下光标依然变黑
布局代码
<com.google.android.material.textfield.TextInputLayout android:id="@+id/input_email_wrapper" style="@style/TextInputLayoutStyle" android:layout_width="0dp" android:hint="@string/email_address" android:labelFor="@id/input_email"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/input_email" style="@style/TextInputEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:afterTextChanged="@={viewModel::emailAfterTextChanged}" android:inputType="textEmailAddress" android:text="@={viewModel.user.email}" tools:text="max@muster.de" /> </com.google.android.material.textfield.TextInputLayout>
ViewModel错误设置代码
binding.inputEmailWrapper.error = getString(R.string.email_invalid)
样式代码
<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="boxStrokeColor">@color/edit_text_border_selector</item> <item name="hintTextColor">@color/white</item> <item name="android:textColorHint">@color/edit_text_border_selector</item> <item name="endIconTint">@color/edit_text_border_selector</item> <item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginTop">@dimen/margin_top</item> <item name="materialThemeOverlay">@style/EditTextTheme</item> </style> <style name="TextInputEditText"> <item name="android:textColor">@color/edit_text_content_color</item> <item name="android:textCursorDrawable">@drawable/edit_text_cursor</item> </style> <style name="EditTextTheme" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox"> <item name="colorError">@color/red</item> <item name="colorPrimary">@color/white</item> <item name="colorControlActivated">@color/white</item> <item name="colorAccent">@color/orange</item> </style>
光标Drawable代码(添加前已存在问题)
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <size android:width="2dp" /> <solid android:color="?base_100" /> </shape>
注:所有颜色选择器均未使用黑色值,已通过截图确认光标为纯黑色。
App主题代码
<style name="AppThemeBJ" parent="Theme.MaterialComponents.NoActionBar"> <!--Some MUST reference color resources instead of direct hex values as in code the color resource for the attribute may be fetched in some cases--> <item name="colorPrimary">@color/darkblue</item> <item name="colorPrimaryDark">@color/darkblue</item> <item name="colorAccent">@color/orange</item> <item name="colorError">@color/red</item> <item name="colorSecondary">@color/orange</item> <item name="colorOnSecondary">@color/white</item> <item name="preferenceTheme">@style/Theme.WtfPreference</item> <item name="android:colorBackground">@color/darkerblue</item> <item name="snackbarStyle">@style/SnackbarStyle</item> <item name="materialTimePickerTheme">@style/TimepickerStyle</item> </style>
状态截图描述
- 错误状态带光标:输入框处于错误状态(红色边框),光标为纯黑色
- 错误状态选中:输入框文本选中区域颜色正常,仅光标为黑色
- 正常状态:光标为白色,输入框显示正常
补充编辑
编辑1:即使移除materialThemeOverlay和textCursorDrawable,错误状态下光标变黑的问题仍存在
编辑2:该Bug仅在OutlinedBox样式下出现,使用以下样式时,错误状态下光标保持激活色(橙色):
<style name="TextInputLayoutStyle" parent="Widget.Design.TextInputLayout"> <item name="hintTextColor">@color/orange</item> <item name="hintTextAppearance">@style/HintTextAppearance</item> <item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginTop">@dimen/margin_top</item> <item name="colorPrimary">@color/orange</item> </style>
内容的提问来源于stack exchange,提问作者Sarah Multitasker
相关产品推荐
相关产品推荐

