如何更改未聚焦的textInputLayout的轮廓或边框颜色?

4

我正在尝试更改textInputLayout的轮廓或边框颜色,但不知道为什么它没有改变。我搜索并找到了一些解决方案,但对我没有用。

这里我放置了样式,然后将其应用于textInputLayout。

<style name="WhiteOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlineBox">
    <item name="boxStrokeColor">@color/snow </item>
    <item name="hintTextAppearance">@style/TextLabel</item>
    <item name="android:textColorHint">@color/snow</item>
    <item name="passwordToggleTint">@color/snow</item>

    <item name="colorControlNormal">@color/snow</item>
    <item name="colorControlActivated">@color/snow</item>
    <item name="colorControlHighlight">@color/snow</item>
    <item name="colorPrimary">@color/snow</item>
    <item name="colorPrimaryDark">@color/snow</item>
    <item name="colorAccent">@color/snow</item>

</style>

<!--  this style for the hint text lable in textInputLayout -->
<style name="TextLabel" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/snow</item>
</style>

这里是在xml中将其应用于textInputLayout的代码

<android.support.design.widget.TextInputLayout
            android:id="@+id/ed_oldPass"
            style="@style/WhiteOutlineBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginTop="50dp"
            app:passwordToggleEnabled="true">

            <android.support.design.widget.TextInputEditText
                style="@style/WhiteOutlineBox"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="@color/snow"
                android:textColor="@color/snow"
                android:layout_marginBottom="10dp"
                android:hint="@string/old_pass_ed_hint"
                android:inputType="textPassword"
                android:paddingEnd="10dp"
                android:paddingStart="10dp" />
        </android.support.design.widget.TextInputLayout>

边框颜色、提示文本外观、提示文本颜色和密码切换色已更改,但其他内容未更改,我想要的是在没有焦点时更改边框颜色,该如何实现?请帮忙,谢谢。


1
到目前为止,我找到的最佳答案是这个 --> https://dev59.com/41UL5IYBdhLWcg3wHU_M#50818399 - Sotti
@Sotti 谢谢,这很有帮助,但如果我想在某些页面上更改它,该怎么做? - Rooh Al-mahaba
1
鉴于所有的东西都很混乱,我不知道该怎么做。我对TextInputLayout缺乏基本自定义功能感到非常惊讶。 - Sotti
3个回答

3
如果想要在编程中仅更改一个TextInputLayout,会出现问题,问题在于属性defaultStrokeColor无法访问,唯一的更改方式是通过覆盖颜色mtrl_textinput_default_box_stroke_color或使用状态列表颜色,但在这两种情况下都需要在XML上设置样式。请注意保留HTML标签。
另一方面,属性focusedStrokeColor可通过setBoxStrokeColor访问,因此可以在不需要任何特殊代码的情况下以编程方式进行更改。
如果反射是您的选择,则有一个解决方案是在运行时更改属性的可访问性。以下代码在material-1.1.0上完成了该工作。
fun TextInputLayout.setDefaultStrokeColor(
    color: Int
) {
    try {
        val defaultStrokeColor = TextInputLayout::class.java.getDeclaredField("defaultStrokeColor")
        defaultStrokeColor.isAccessible = true
        defaultStrokeColor.set(this, color)
    } catch (e: NoSuchFieldException) {
        // failed to change the color
    }
}

将其用作扩展函数

yourView.setDefaultStrokeColor(yourColor)

1
我正在寻找的东西,谢谢! - Alan Bastos

3
你可以使用 boxStrokeColor 属性。它可以与选择器一起使用。
类似这样使用:
```css selector { boxStrokeColor: color; } ```
    <com.google.android.material.textfield.TextInputLayout
        app:boxStrokeColor="@color/text_input_layout_stroke_color"
        ..>

或者
<style name="WhiteOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlineBox">
    <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
</style>

使用:

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

enter image description here
enter image description here
enter image description here


1
将此添加到您的color.xml文件中。
<color name="mtrl_textinput_default_box_stroke_color">#BDC3C7</color>

它覆盖了默认的未聚焦轮廓颜色。

2
但所有的Textinputlayout更改。不针对特定的活动主题。 - ulaserdegor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接