【问题标题】:Multicolored edittext hint多彩多姿的编辑文本提示
【发布时间】:2017-03-07 11:28:58
【问题描述】:

当被 android.support.design.widget.TextInputLayout 包裹时,有没有办法在不影响浮动编辑文本的行为的情况下为编辑文本的提示设置多种颜色?

喜欢, 标题*

标题和 * 带有不同颜色的提示

【问题讨论】:

  • 也许this 解决方案也适用于提示。
  • 感谢您的回答。但我想为 TextInputLayout 设置提示,而不是为 Edittext 设置提示。因为我需要提示文本的浮动动画。
  • 你用TextWatcher试过这个
  • 嘿@RKS 你试过我的答案了吗?

标签: android android-edittext hint textinputlayout


【解决方案1】:

试试这个!如果你想将它用于 setText 只需添加 BufferType.SPANNABLE (见下文)

    String redPart = "Hello";
    String bluePart = "World";

    SpannableStringBuilder builder = new SpannableStringBuilder();

    SpannableString redColoredString= new SpannableString(redPart);
    redColoredString.setSpan(new ForegroundColorSpan(Color.RED), 0, redPart.length(), 0);
    builder.append(redColoredString);

    SpannableString blueColoredString= new SpannableString(bluePart);
    blueColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, bluePart.length(), 0);
    builder.append(blueColoredString);


    myEditText.setHint(builder)

    //do following for setText
    myEditText.setText(builder,BufferType.SPANNABLE)

【讨论】:

  • 很好的答案!谢谢!
【解决方案2】:

您可以通过编程方式实现这一目标

SpannableString spString = new SpannableString("HEADERLINE*");
    spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 11,     spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    metUserName.setHint(spString);

【讨论】:

    【解决方案3】:

    查看下面的代码,这对我有用。

     EditText editText = (EditText) findViewById(textView);
     Spannable wordtoSpan = new SpannableString("Hello world");
    
     wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
     editText.setHint(wordtoSpan);
    

    【讨论】:

    • 如果应用此方法,提示不会浮到顶部。有什么办法可以克服吗?
    【解决方案4】:

    使用SpannableString 类,它允许您在部分字符串上使用不同的样式...如果我没记错的话,TextAppearanceSpan 类用于为文本着色..

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 2014-01-07
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多