43

使用设计支持库中的 TextInputLayout 时,是否可以仅将提示中的星号设为红色?我已经看到有关样式化整个提示的信息,但这有点复杂,因为只有 * 应该是红色的,而不是整个消息。

Material Design 示例显示了这一点,但设计库似乎没有任何选项可以使用 TextInputLayout 和 EditText 以这种方式设置样式。

参考: https ://www.google.com/design/spec/components/text-fields.html#text-fields-required-fields

示例(有焦点的顶部有红色星号;没有焦点的底部没有红色星号):

带有红色星号的提示文本示例

我研究了在 OnFocusChangeListener 中将提示设置为 SpannableString (请参阅此处如何在 <string> 条目中获取红色星号)(请参阅此处在 textinputlayout 内的编辑文本(红色星号)中具有强制符号) ,但提示是一个 CharSequence。

有没有办法在不扩展 TextInputLayout 的情况下做到这一点?

4

8 回答 8

20

Material Design 为作为提示文本的一部分的必填字段指定一个星号,并且颜色相同(不像您在问题中显示的那样为红色)。

在 Kotlin 中,这非常简单:

1.定义这个扩展方法:

fun TextInputLayout.markRequired() {
    hint = "$hint *"
}

2.使用它:

input_first_name.markRequired()

如果您仍然想要星号红色,尽管 Material Design 指南不鼓励您,您可以这样使用 AndroidX Core KTX:

fun TextInputLayout.markRequiredInRed() {
    hint = buildSpannedString {
        append(hint)
        color(Color.RED) { append(" *") } // Mind the space prefix.
    }
}
于 2017-09-27T14:10:57.800 回答
18

您可以使用材料组件库。
从 开始,1.2.0-alpha05您可以格式化提示文本。

例如,您可以使用类似的东西:

<com.google.android.material.textfield.TextInputLayout
    android:hint="@string/hint_test"
    ...>

和:

<string name="hint_test">Legal name <font color='#FF0000'>*</font></string>

在此处输入图像描述在此处输入图像描述

于 2020-05-05T20:49:40.623 回答
3

你能从 Html 添加一个跨越的字符串吗?

   String grey = "Legal first name*";
   Spanned redStar;
   String html = "<string style="color:grey;">Legal first name<span style="color:red;">*</span></string>";

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
   redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
   } else {
   redStar = Html.fromHtml(html);
   }

并在焦点上更改提示。

   textInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
   public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus)
        myEditText.setHint(redStar.toString);
    else
        myEditText.setHint(grey);
}
于 2017-06-05T10:51:55.097 回答
2
 <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>

改变你的主题 colorAccent 看看

于 2016-12-08T11:12:16.367 回答
1

通过添加包含在 html 中的后缀来更改 TextInputLayout 提示:

public void setRequired(boolean required) {

  componentField.setHint(TextUtils.concat(
      componentField.getHint(),
      Html.fromHtml(getContext().getString(
          R.string.required_asterisk))));
}

星号代码应存储在 android strings.xml 中以进行正确转义:

<string name = "required_asterisk"><![CDATA[<font color=\'#cc0029\'>&nbsp*</font>]]></string>
于 2017-07-25T10:32:46.257 回答
0

这对我有用

textinputlayout.setHint(Html.fromHtml("Project Title <font color =\"#cc0029\" >*</font>"));
于 2021-01-30T06:58:13.313 回答
-1

我经历了同样的过程,它对我有用

 TextView text = (TextView) rootView.findViewById(R.id.name_textview);

 SpannableStringBuilder builder1 = setStarToLabel("Name");

 text.setText(builder1);

  @NonNull
private SpannableStringBuilder setStarToLabel(String text) {
    String simple = text;
    String colored = "*";
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(simple);
    int start = builder.length();
    builder.append(colored);
    int end = builder.length();
    builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return builder;
}
于 2017-05-24T06:26:13.167 回答
-1

是的,它可能,因为我在我当前的应用程序中做同样的事情。对于专注和不专注,您必须执行一些逻辑来删除星号。

String mm = "Legal first name";
Spannable WordtoSpan = new SpannableString(mm);
WordtoSpan.setSpan(
        new ForegroundColorSpan(Color.parseColor("#e62e6c")), 
        16, 
        mm.length(), 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new RelativeSizeSpan(0.9f), 16, mm.length(), 0);
view.setText(WordtoSpan);
于 2016-12-08T11:27:58.967 回答