如何使TextInputLayout提示星号为红色以显示必填字段

问题描述 投票:32回答:6

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

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

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

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

Example of hint text with red asterisk

我查看了在OnFocusChangeListener中将提示设置为SpannableString(请参阅此处How to get a red asterisk in a <string> entry)(请参阅此处Having the mandatory symbol to the edit text (red color asterisk) which is inside the textinputlayout),但提示是CharSequence。

有没有办法在不扩展TextInputLayout的情况下执行此操作?

android android-edittext material-design android-textinputlayout
6个回答
10
投票

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.
    }
}

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,看看


2
投票

你能从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);
}

0
投票

通过添加包含在html中的postfix来更改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>

-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);

-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;
}
© www.soinside.com 2019 - 2024. All rights reserved.