Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

colors - Android EditText + set some words in colour

I create EditText boxes dynamically, depending on what is pulled back from the database.

Ie.

            final EditText textV = new EditText(this);
            textV.setText(monEntry);

Is there anyway to set some of the text in the EditText to one colour and another bit to another without using a subString()? Thanks alot if anybody can help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you can have different colors in different places of the text if you are using SpannableString. Example:

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
final EditText textV = new EditText(this);
// make "Lorem" (characters 0 to 5) red  
textV.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
textV.setText(text, BufferType.SPANNABLE);

or you can use html code as below::

textV.setText(Html.fromHtml(html text having 1 in red 2 in green and so on));

There's a more complete example here.

Javadoc for SpannableString


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...