Pro*_*man 34 android android-edittext material-design android-textinputlayout
当使用设计支持库中的TextInputLayout时,是否可以在提示中只显示星号?我已经看到了关于样式整个提示的信息,但这有点复杂,因为只有*应该是红色,而不是整个消息.
Material Design示例显示了这一点,但设计库似乎没有任何选项可以使用TextInputLayout和EditText以这种方式设置样式.
参考:https: //www.google.com/design/spec/components/text-fields.html#text-fields-required-fields
示例(顶部,有焦点,有红色星号;没有焦点的底部没有红色星号):
我查看将提示设置为一个SpannableString(请参阅此处如何在<string>条目中获取红色星号)在OnFocusChangeListener中(请参阅此处具有编辑文本的强制符号(红色星号),该符号位于textinputlayout内) ,但提示是CharSequence.
有没有办法在不扩展TextInputLayout的情况下执行此操作?
Gab*_*tti 11
您可以使用材料组件库。
从1.2.0-alpha05您可以格式化提示文本开始。
例如,您可以使用以下内容:
<com.google.android.material.textfield.TextInputLayout
android:hint="@string/hint_test"
...>
Run Code Online (Sandbox Code Playgroud)
和:
<string name="hint_test">Legal name <font color='#FF0000'>*</font></string>
Run Code Online (Sandbox Code Playgroud)
Lou*_*CAD 10
Material Design为必需字段指定星号,该字段是提示文本的一部分,并且颜色相同(不是您在问题中显示的红色).
在Kotlin这很简单:
1.定义这个扩展方法:
fun TextInputLayout.markRequired() {
hint = "$hint *"
}
Run Code Online (Sandbox Code Playgroud)
2.使用它:
input_first_name.markRequired()
Run Code Online (Sandbox Code Playgroud)
如果你仍然希望星号是红色的,尽管不受Material Design准则的劝阻,你可以这样使用AndroidX Core KTX:
fun TextInputLayout.markRequiredInRed() {
hint = buildSpannedString {
append(hint)
color(Color.RED) { append(" *") } // Mind the space prefix.
}
}
Run Code Online (Sandbox Code Playgroud)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)
改变你的主题颜色强调并看看
| 归档时间: |
|
| 查看次数: |
10889 次 |
| 最近记录: |