美文网首页
【原创】全方位修改textlayout属性 解决未选中状态下划线

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

作者: 吉凶以情迁 | 来源:发表于2021-11-27 09:39 被阅读0次
         <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>
    

    相关文章

      网友评论

          本文标题:【原创】全方位修改textlayout属性 解决未选中状态下划线

          本文链接:https://www.haomeiwen.com/subject/kdcyaltx.html