如何更改未聚焦的 textInputLayout 的轮廓或边框的颜色?
how to change the color of outline or border of textInputLayout not focused?
我正在尝试更改 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>
描边颜色、hintTextAppearence、提示颜色和通过切换色调颜色 已更改 但其他没有,我想要的是在边框未聚焦时更改边框颜色如何做到这一点?请帮助并提前致谢
将此添加到您的 color.xml
<color name="mtrl_textinput_default_box_stroke_color">#BDC3C7</color>
它会覆盖默认的未聚焦轮廓颜色
如果只想以编程方式更改一个 TextInputLayout 就会出现问题,问题是 属性 defaultStrokeColor is not accessible, the only way to change it is through overriding the colour mtrl_textinput_default_box_stroke_color or uses a state list colour 但在这两种情况下,您都需要 XML 上的样式。
另一方面,属性 focusedStrokeColor is accessible through setBoxStrokeColor,因此可以通过编程方式更改它而无需任何特殊代码。
它的一个解决方案,如果反射是你的选择,当然是改变 run-time 中 属性 的可访问性,下面的代码在 [=24] 上完成工作=]-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
}
}
将其用作 extension function:
yourView.setDefaultStrokeColor(yourColor)
您可以使用 boxStrokeColor
attribute.It 可以使用选择器。
使用类似于:
<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>
我正在尝试更改 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>
描边颜色、hintTextAppearence、提示颜色和通过切换色调颜色 已更改 但其他没有,我想要的是在边框未聚焦时更改边框颜色如何做到这一点?请帮助并提前致谢
将此添加到您的 color.xml
<color name="mtrl_textinput_default_box_stroke_color">#BDC3C7</color>
它会覆盖默认的未聚焦轮廓颜色
如果只想以编程方式更改一个 TextInputLayout 就会出现问题,问题是 属性 defaultStrokeColor is not accessible, the only way to change it is through overriding the colour mtrl_textinput_default_box_stroke_color or uses a state list colour 但在这两种情况下,您都需要 XML 上的样式。
另一方面,属性 focusedStrokeColor is accessible through setBoxStrokeColor,因此可以通过编程方式更改它而无需任何特殊代码。
它的一个解决方案,如果反射是你的选择,当然是改变 run-time 中 属性 的可访问性,下面的代码在 [=24] 上完成工作=]-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
}
}
将其用作 extension function:
yourView.setDefaultStrokeColor(yourColor)
您可以使用 boxStrokeColor
attribute.It 可以使用选择器。
使用类似于:
<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>