如何更改TextInputLayout散焦的边框和颜色?

时间:2020-02-05 15:34:34

标签: android android-xml android-textinputlayout material-components-android material-components

如何在providers: [provideRoutes(routes)], 散焦状态下更改边框和颜色?

TextInputLayout

我尝试:

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_height="wrap_content"
        app:boxStrokeWidth="4dp"
        app:boxStrokeColor="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@id/a">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:hint="@string/app_name"
            android:textColorHint="@color/colorPrimary"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

但是,不能专心工作

2 个答案:

答案 0 :(得分:6)

如果要在聚焦/非聚焦模式下具有不同的笔划宽度,可以使用 boxStrokeWidth boxStrokeWidthFocused 属性。

<!-- The value to use for the box's stroke when in outline box mode, 
     or for the underline stroke in filled mode. -->
<attr format="dimension" name="boxStrokeWidth"/>
<!-- The value to use for the focused box's stroke when in outline box mode,
     or for the focused underline stroke in filled mode.. -->
<attr format="dimension" name="boxStrokeWidthFocused"/>

类似的东西:

    <com.google.android.material.textfield.TextInputLayout
        app:boxStrokeWidthFocused="4dp"
        app:boxStrokeWidth="1dp"
        app:boxStrokeColor="@color/text_input_layout_stroke_color"
        ..>

对于笔触颜色,您可以定义一个选择器,例如:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_focused="true"/>
  <item android:alpha="0.87" android:color="@color/..." android:state_hovered="true"/>
  <item android:color="@color/.." android:state_enabled="false"/>
  <item android:color="@color/..."/> <!--unfocused-->
</selector>

enter image description here enter image description here

注意boxStrokeWidthFocused至少需要版本1.2.0-alpha04

答案 1 :(得分:-1)

更改边界; 对 TextInputLayout 的背景使用可绘制的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorRipple">
    <item
        android:state_enabled="true"
        android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <stroke android:color="#aA0000" android:width="1dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#dddddd" />
            <stroke android:color="#ff00ff" android:width="1dp"/>
        </shape>
    </item>
</selector>

更改文字颜色; 将可绘制选择器用于 TextInputEditText 的文本颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#333333" android:state_selected="true" />
    <item android:color="#333333" android:state_focused="true" />
    <item android:color="#009900" /> <!-- default case -->
</selector>