【原创】全方位修改textlayout属性 解决未选中状态下划线颜色修改问题

<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>
<!--        然并卵解决不了问题,boxStrokeColor其实用选择器就可以解决 未获取焦点的描边色问题-->
<!--        <item name="boxStrokeColor">@color/red</item>-->
        <item name="boxStrokeWidth">1dp</item>

selector

<?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>
<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

<?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>

搞错,状态只支持如下

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();
    }

最后

<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值