我想将 TextView 中除数字和逗号之外的任何字母设置为白色 然后我想将其他字符的颜色设置为 White 。 我该怎么做?
示例
19,319,931 coins
//19,319,931 should has yellow color.
//coins should has has white color
最佳答案
您可以使用Spannable TextView
可扩展的 TextView 可在 Android 中使用,在单个 TextView 小部件中以不同的颜色、样式、大小和/或点击事件突出显示文本的特定部分。
所以试试这个:
String coinText = txtDiamonds.getText().toString();
char currentChar;
Spannable spannable = new SpannableString(coinText);
ForegroundColorSpan color;
for (int i = 0; i < coinText.length(); i++) {
currentChar = coinText.charAt(i);
if (Character.isDigit(currentChar) || ((int) currentChar) == ((int) ','))
color = new ForegroundColorSpan(Color.YELLOW);
else
color = new ForegroundColorSpan(Color.WHITE);
spannable.setSpan(color, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
txtDiamonds.setText(spannable);
关于java - android中除了数字和逗号之外,如何将TextView中的任何字母设置为白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118390/