java - android中除了数字和逗号之外,如何将TextView中的任何字母设置为白色?

标签 java android android-layout textview

我想将 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/

相关文章:

android - Cardview 中的 ConstraintLayout 添加空白

java - 将java小程序转换为java应用程序

java - 如何在 Azure 的 Spring Cloud Functions 中 Autowiring 我的 Bean?

java - 在 JSF Converter 类的 getAsObject() 和 getAsString() 方法中转换什么值

android - Google+登录redirect_uri_mismatch错误

javascript - react native : elevation (shadow) and transparency on button don't work together as needed

android - 水平回收器 View 不显示任何内容

java - Logstash 未将完整数据从 oracle 加载到 Elasticsearch

android - RxJava 使用 zip 链接多个调用

android - 选择为 textView 和 editText 添加边框形状时,