How to get a red asterisk in a entry

前端 未结 4 996
情书的邮戳
情书的邮戳 2020-12-31 10:56

How do you get a red asterisk in a entry so that you can display it at the end of the text to indicate its a required field, like: Enter your name * (asterisk will be red).

4条回答
  •  梦毁少年i
    2020-12-31 11:23

    You can't do that through xml string resources. This can only be done via code. For this you need to use SpannableStringBuilder and ForegroundColorSpan.


    Here is small example:

    TextView text = (TextView)findViewById(R.id.text);
    
    String simple = "Enter your name ";
    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);
    
    text.setText(builder);
    

    enter image description here

提交回复
热议问题