I am trying to make the text of an EditText multiple colors. For example, if my text is, "It is a good day.", is it possible to make the "It is a" part of the sentence green and the rest red?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You could use spannables.
I'm have this trouble too. After several hours, I figured out how to handle it:
This code make first 10 characters in black color and the followed characters in red color. We need variable oldContent to prevent loop infinity because when EditText call setText() then onTextChanged
I use something like that to make some parts of my color green:
Yes. You will need to create a
Spannable
object (either aSpannedString
orSpannedStringBuilder
), then set spans upon it to apply the colors you seek.For example, the following method from this sample project takes the contents of a
TextView
, searches for a user-entered string, and marks up all occurrences with a purple background color, removing all previous markers:In your case, changing the foreground color would use a
ForegroundColorSpan
instead of aBackgroundColorSpan
.Things get a bit tricky with an
EditText
, in that the user can edit the text, and you will need to choose your flags to meet the rules you want. For example,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
would say that: