android - 文本输入布局 : Set box stroke color programmatically (highlighted and unhighlighted)

标签 android android-textinputlayout material-components material-components-android

我正在尝试将这些 Material 文本字段与框轮廓样式一起使用。我看到有一个名为 boxStrokeColor 的属性,它可以让我设置笔触仅当文本字段突出显示时 当它没有突出显示时,它似乎是从全局主题的 colorPrimary 值。

在我们的应用中,用户可以为某些 View 设置自己的背景颜色,我想将文本字段的描边颜色更改为合适的对比色。

是否有任何干净的方法以编程方式设置未突出显示的框描边颜色?

最佳答案

您可以使用 setBoxStrokeColorStateList 方法。 像这样的东西:

textInputLayout.setBoxStrokeColorStateList(AppCompatResources.getColorStateList(this,R.color.text_input_layout_stroke_color));

它与选择器一起工作:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>

关于android - 文本输入布局 : Set box stroke color programmatically (highlighted and unhighlighted),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54222403/

相关文章:

android - 如何通过代码圆化 AlertDialog 的角

android - 启动按钮从右到左而不是从左到右

android - 在 Android 上过滤 Firebase 数据库 DataSnapshot 子级

android - 原语 Realm 列表的 Gson 反序列化

android - actionNext 不会滚动显示最后一个 TextInputLayout

android - 文本输入布局颜色

java - 如何将图库中选定的图像塑造为圆形

Android:EditText 的 KeyListener 未接收 key

android - Android Studio 中的自动代码格式化

android - 更改 float 标签 EditText 和 TextInputLayout 的字体