【问题标题】:how to change border and color in TextInputLayout unfocus?如何在 TextInputLayout unfocus 中更改边框和颜色?
【发布时间】:2020-05-21 13:37:06
【问题描述】:

TextInputLayoutunfocus如何改变边框和颜色?

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

我试试:

app:boxStrokeWidth="4dp"
app:boxStrokeColor="@color/colorPrimary"

但是,在散焦状态下不起作用

【问题讨论】:

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


    【解决方案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 的 textcolor

    使用可绘制选择器
    <?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>
    

    【讨论】:

    • 让我知道出了什么问题?我已经对此进行了测试,并且可以正常工作!您是否为 TextInputLayout 添加了正确的可绘制对象?
    • 如果我们使用 TextInputLayout 我们应该利用像 setBoxStrokeColorStateList 这样的方法而不是手动设置背景
    【解决方案2】:

    如果您想在聚焦/不聚焦模式下拥有不同的笔画宽度,您可以使用 boxStrokeWidthboxStrokeWidthFocused 属性。

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

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

    【讨论】:

    • 如果焦点使用 app:boxStrokeColor,我已经更改了框颜色。但是,如果没有焦点,如何更改框颜色?
    • @AfdalDev 您必须使用答案中报告的选择器。您可以为不同的状态定义不同的颜色。
    猜你喜欢
    • 2021-01-11
    • 2019-02-10
    • 2019-08-22
    • 1970-01-01
    • 2019-02-03
    • 2015-02-06
    • 1970-01-01
    • 2020-01-12
    • 2013-02-18
    相关资源
    最近更新 更多