简书链接:【原创】全方位修改textlayout属性解决未选中状态下划线颜色修改问题 文章字数:120,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <com.google.android.material.textfield.TextInputLayout android:id="@+id/textinputlayout" android:layout_width="match_parent" android:hint="@{title}" app:hintTextColor="@color/quantum_white_hint_text" style="@style/CustomAppTheme_textinputLayout" app:boxStrokeColor="@color/white_textinputlayout_box_stroke" app:boxBackgroundColor="@color/transparent" android:textColorHint="@color/quantum_white_secondary_text" app:placeholderTextColor="@color/quantum_white_100" android:textColor="@color/white_no_night" android:layout_height="wrap_content" > <androidx.appcompat.widget.AppCompatEditText android:id="@+id/edittext" android:textSize="@dimen/app_edit_textsize" android:tag="@{tag}" android:layout_width="match_parent" android:background="@color/transparent" android:layout_height="wrap_content" android:editable="false" android:lines="1" android:inputType="none" android:textColor="@color/white_no_night" app:hintTextColor="@color/quantum_white_hint_text" android:text="@{date}" android:imeOptions="actionNext|flagNoExtractUi" /> </com.google.android.material.textfield.TextInputLayout>
1 2 3 <!-- 然并卵解决不了问题,boxStrokeColor其实用选择器就可以解决 未获取焦点的描边色问题--> <!-- <item name="boxStrokeColor">@color/red</item>--> <item name="boxStrokeWidth">1dp</item>
selector
1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/white_no_night" android:state_activated="true"></item> <item android:color="@color/white_no_night" android:state_focused="true"></item> <item android:color="@color/white_no_night" android:state_hovered="true"></item> <item android:color="@color/white_transparency" android:state_activated="false"></item> <item android:color="@color/white_transparency" android:state_focused="false"></item> <item android:color="@color/white_transparency" android:state_hovered="false"></item> <item android:color="@color/white_transparency" android:state_focused="false"></item> <!-- 改选择器解决 https://www.nuomiphp.com/eplan/19445.html 网上的方法会改变所有,而目前这种方法才是最好用的--> </selector>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <com.google.android.material.textfield.TextInputLayout android:id="@+id/textinputlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@{title}" android:textColor="@color/white_no_night" android:textColorHint="@color/quantum_white_secondary_text" app:boxStrokeColor="@color/white_textinputlayout_box_stroke_selector" app:boxStrokeErrorColor="@color/green" app:counterTextColor="@color/green" app:helperTextTextColor="@color/green" app:hintTextColor="@color/yellow_50" app:placeholderTextColor="@color/quantum_white_100" app:prefixTextColor="@color/green" app:suffixTextColor="@color/green"> <androidx.appcompat.widget.AppCompatEditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@color/transparent" android:focusable="true" android:focusableInTouchMode="true" android:imeOptions="actionNext" android:lines="1" android:singleLine="true" android:textColorHighlight="@color/black" android:tag="@{tag}" android:text="无" android:textSelectHandle="@drawable/textslelct_handler" android:textSelectHandleLeft="@drawable/textslelct_handler" android:textSelectHandleRight="@drawable/textslelct_handler" android:selectAllOnFocus="true" android:textColor="@color/white_no_night" android:textColorHint="@color/quantum_white_hint_text" android:textCursorDrawable="@drawable/text_cursor_drawable" android:textIsSelectable="true" android:textSize="@dimen/app_edit_textsize" app:hintTextColor="@color/white" app:text="@{content}" /> </com.google.android.material.textfield.TextInputLayout>
white_textinputlayout_box_stroke_selector
1 2 3 4 5 6 7 8 9 10 11 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/white_no_night" android:state_activated="true"></item> <item android:color="@color/white_no_night" android:state_focused="true"></item> <item android:color="@color/white_no_night" android:state_hovered="true"></item> <item android:color="@color/white_transparency" android:state_activated="false"></item> <item android:color="@color/white_transparency" android:state_focused="false"></item> <item android:color="@color/white_transparency" android:state_hovered="false"></item> <item android:color="@color/white_transparency" android:state_focused="false"></item> </selector>
搞错,状态只支持如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 if (boxStrokeColorStateList.isStateful()) { defaultStrokeColor = boxStrokeColorStateList.getDefaultColor(); disabledColor = boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1); hoveredStrokeColor = boxStrokeColorStateList.getColorForState( new int[] {android.R.attr.state_hovered, android.R.attr.state_enabled}, -1); focusedStrokeColor = boxStrokeColorStateList.getColorForState( new int[] {android.R.attr.state_focused, android.R.attr.state_enabled}, -1); } else if (focusedStrokeColor != boxStrokeColorStateList.getDefaultColor()) { // If attribute boxStrokeColor is not a color state list but only a single value, its value // will be applied to the box's focus state. focusedStrokeColor = boxStrokeColorStateList.getDefaultColor(); }
最后
1 2 3 4 5 <item android:color="@color/red" android:state_focused="false"></item> <item android:color="@color/red" android:state_focused="false"></item> <item android:color="@color/red" android:state_enabled="false"></item> <item android:color="@color/themeColor" android:state_focused="true"></item> <item android:color="@color/themeColor" android:state_hovered="true"></item>