如何更改 Android 中 TextInputLayout 的 boxStrokeColor?

How to change boxStrokeColor Of TextInputLayout in Android?

我使用以下代码动态创建 textInputlayout 并将其添加到我的 lienarlayot:

        LinearLayout.LayoutParams params = new 
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10,10,10,10);
        til.setLayoutParams(params);
        til.setHint("Label");
        til.setId(i+1);
        TextInputEditText et = new TextInputEditText(til.getContext());  
        et.setSingleLine(true);
        til.addView(et);
        til.setBoxCornerRadii(R.dimen.CornerRadious,R.dimen.CornerRadious,R.dimen.CornerRadious,
        R.dimen.CornerRadious);
        information.addView(til);

我想更改 boxStrokeColor 、 CursorColor 和 HintTextColor。

我使用下面的代码块来改变boxStrokeColor

 til.setBoxStrokeColor(getResources().getColor(R.color.white));

但是当我点击 TextInputLayout 时,那个代码块只改变了 TextInputLayout 的 boxStrokeColor(白色),没有点击它的颜色是黑色。如何设置它的颜色为白色?

您必须使用选择器(ColorStateList),否则只有在获得焦点时才会将单个值应用于框。

使用方法setBoxStrokeColorStateList

til.setBoxStrokeColorStateList(ContextCompat.getColorStateList(this.R.color.selector))

其中 selector 类似于:

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

注意: 它至少需要 material 组件库的版本 1.2.0

如果您想使用 xml:

中定义的样式,还有一个替代方案

attrs.xml 中定义自定义属性:

<attr name="customTextInputStyle" format="reference" />

然后在你的主题中添加这个属性:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
    <!-- ..... -->
    <item name="customTextInputStyle">@style/Widget.App.TextInputLayout.OutlinedBox</item>
</style>

现在您可以添加新样式中的所有属性:

<style name="Widget.App.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/boxstroke_selector</item>
    <!-- .... -->
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.TextInputEditText.OutlinedBox</item>
</style>

<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox"
    parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <!-- to change the cursor color -->
    <item name="colorControlActivated">@color/teal_200</item>
</style>

最终创建您的 TextInputLayout

TextInputLayout til = new TextInputLayout(this,null,R.attr.customTextInputStyle);

我建议使用 XML 而不是全部使用代码。 您必须在 colors.xml 文件中添加此行,这将覆盖轮廓框的默认颜色。

按原样复制此行。您可以根据需要更改颜色:

<color name="mtrl_textinput_default_box_stroke_color">#fff</color>

直到现在它都可以工作,要对 TextInputLayout 进行更多控制,您可以在 styles.xml

中添加此样式
<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">#000</item>
    <item name="boxStrokeWidth">2dp</item>
    <item name="hintTextColor">#000</item>
    <item name="android:theme">@style/exampleCursor</item>
</style>

<style name="exampleCursor" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="colorControlActivated">@color/green</item>
</style>

然后添加主题到TextInputLayout:

style="@style/TextInputLayoutStyle"