gpt4 book ai didi

java - 创建颜色函数

转载 作者:行者123 更新时间:2023-12-01 14:11:51 24 4
gpt4 key购买 nike

我需要你的帮助来制作一个颜色函数。我想创建这样的东西

我有一个 edittext 并且我想放入 string 的颜色部分,但有一个特殊的输入示例:

if the user type : ^1Hi ^2Stack^3Over^4Flow !
"Hi" = red color , "Stack" = green color , "Over" = blue color and "Flow" = orange color.

如何做到这一点?

最佳答案

enter image description here您可以使用 Spannable 字符串来实现此目的:

SpannableStringBuilder builder = new SpannableStringBuilder();

String red = "RedText";
SpannableString str1= new SpannableString(red);
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(str1);

String white = "WhiteText";
SpannableString str2= new SpannableString(white);
str2.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
builder.append(str2);

String blue = "BlueText";
SpannableString str3 = new SpannableString(blue);
str3.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
builder.append(str3);

mTextView.setText(builder, BufferType.SPANNABLE);

关于java - 创建颜色函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com