简体   繁体   English

为 android.support.design.widget 更改 TextInputLayout 的 boxBackgroundColor

[英]change boxBackgroundColor of TextInputLayout for android.support.design.widget

I want to change dynamically boxBackgroundColor of TextInputLayout when editText.setEnables(false) .我想在editText.setEnables(false)时动态boxBackgroundColor TextInputLayout 的editText.setEnables(false) I've tried to do this via res/color/selector_input_background_color but it didn't work.我试图通过 res/color/selector_input_background_color 来做到这一点,但没有奏效。 Also TextInputLayout ( android.support.design.widget.TextInputLayout ) doesn't have such parameter as boxBackgroundColor ! TextInputLayout ( android.support.design.widget.TextInputLayout ) 也没有boxBackgroundColor这样的参数! ;( - it's ONLY preset in AndroidX library ( com.google.android.material.textfield.TextInputLayout ), that I'm afraid to migrate to right now. (Old project) ;( - 它仅在 AndroidX 库( com.google.android.material.textfield.TextInputLayout )中预设,我害怕现在迁移到。(旧项目)

here is how my layout look like这是我的布局的样子

<android.support.design.widget.TextInputLayout
        [...]
        android:theme="@style/TextInputLayoutTheme"
        style="@style/TextInputLayoutStyle">

        <android.support.design.widget.TextInputEditText
             [...]
             />

</android.support.design.widget.TextInputLayout>

TIL inherit styles from new MaterialDesign TIL 从新的 MaterialDesign 继承样式

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="boxBackgroundColor">@color/selector_input_background_color</item>
</style>

And depending on state I provided selector inside res/color根据状态,我在 res/color 中提供了选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#00FF00"/>
    <item android:color="@color/editTextBackground"/>
</selector>

I'm afraid this is a bug, because Google promise to keep compability with 28.0.0 support library version and AndroidX.恐怕这是一个错误,因为谷歌承诺保持与 28.0.0 支持库版本和 AndroidX 的兼容性。 What's funny even if TextInputLayout doesn't have method such as .setBoxBackgroundColor it works inside style - but selector doesn't.即使 TextInputLayout 没有诸如.setBoxBackgroundColor之类的方法,它也可以在样式内工作,但选择器却没有。

I suspect due to this gap, even if I have selector boxBackgroundColor it is not called when EditText change state:enabled , because TextInputLayout don't know that parameter - however it somehow magically used at the begin when the layout is loaded.我怀疑由于这个差距,即使我有选择器 boxBackgroundColor 它也不会在 EditText 更改state:enabled时调用,因为 TextInputLayout 不知道该参数 - 但是它在加载布局时以某种方式神奇地在开始时使用。

Currently there is only workaround for it.目前只有解决方法。

You can use support library as long as You want to keep the same box color all the time.只要您想始终保持相同的框颜色,就可以使用支持库。

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="boxBackgroundColor">@color/selector_input_background_color</item>
</style>

However if You want to change its color (eg that indicated that field is disabled), You have to change it programatically.但是,如果您想更改其颜色(例如,表明该字段已禁用),则必须以编程方式更改它。 But there is one hitch.但有一个障碍。 You have to switch to androidX , because in android.support there is lack of this method.您必须切换到androidX ,因为在 android.support 中缺少此方法。

So You have to do所以你必须做

textInputLayoutPhone.setBoxBackgroundColorResource(R.color.editTextDisableBackground);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 库android.support.design.widget错误 - Error with library android.support.design.widget 找不到 android.support.design.widget 类 - android.support.design.widget class not found 在Xamarin中使用android.support.design.widget - Use android.support.design.widget in Xamarin 包 android.support.design.widget 不存在 - package android.support.design.widget does not exist 错误:包 android.support.design.widget 不存在 - error: package android.support.design.widget does not exist 更改TabLayout(android.support.design.widget)的选定选项卡指示器颜色 - change the selected tab indicator color of TabLayout(android.support.design.widget) 错误包 android.support.design.widget 不存在 - error package android.support.design.widget does not exist Crosswalk Xwalk Webview冲突CollapsingToolbarLayout <android.support.design.widget> - Crosswalk Xwalk Webview conflict CollapsingToolbarLayout<android.support.design.widget> 错误:包 android.support.design.widget 不存在 import android.support.design.widget.FloatingActionButton; - error: package android.support.design.widget does not exist import android.support.design.widget.FloatingActionButton; 我的应用程序也使用一次时崩溃 - My app crash when use also once <android.support.design.widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM