如何在Android中更改TextInputLayout的boxStrokeColor?

3

我使用以下代码动态创建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时才会更改boxStrokeColor的颜色(变成白色),而没点击它时它的颜色是黑色。我该怎么将它的颜色设置为白色?


请返回翻译文本:同时发布您初始化til和使用的应用程序主题的第一行。 - Gabriele Mariotti
2个回答

2

你必须使用选择器(ColorStateList),否则单个值只会在焦点状态下应用于框。

使用方法setBoxStrokeColorStateList

til.setBoxStrokeColorStateList(ContextCompat.getColorStateList(this.R.color.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>

enter image description here

enter image description here enter image description here

注意: 它需要至少材料组件库的版本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);

enter image description here


我的盒子颜色没有改变,仍然是黑色。 - Neo
当我将材料版本从1.1.0更改为1.2.0或1.3.0时,我遇到了两个渲染问题。 - Neo
@Neo 你应该更好地检查更新库中的问题。无论如何,您仍然可以使用在xml中定义的样式。 - Gabriele Mariotti
在1.1.0版本中,我可以通过单击输入textInputlayouta,但在更改版本后,我必须点击两次才能输入数据,而我创建的textInputlayout的格式也被破坏了。 - Neo
如何更改光标颜色?我使用以下代码:<style name="Widget.App.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> @color/white 但是没有生效。 - Neo
显示剩余3条评论

0

我建议使用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"

我动态创建了一些内容,因此无法使用android:theme="@style/TextInputLayoutStyle"。 - Neo

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