如何为Android中用edittext编写的每个字符设置不同的颜色? [英] How to set different colors for each characters written in edittext in Android?
问题描述
我正在尝试使用 edittext
编写的字符具有不同的颜色.例如,当我用 edittext
编写代码时,我一直希望它始终为红色,而其他字符保持黑色.至少有可能吗?
I am trying make characters written in edittext
colored with a different color.
For example, I want to have 'a' in red all the time when it is written in edittext
, while the others stay in black. Is it possible at least?
我在下面的 edittext
中找到了一些有关颜色设置的答案,但是,关于颜色设置的所有方面都取决于开始索引和结束索引的范围.
I found some answers regarding color-setting in edittext
as below, however it is all about color-setting by the range of start index and end index.
**示例**
final String text = "for example";
Spannable modifiedText = new SpannableString(text);
modifiedText.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(modifiedText);
推荐答案
是可以的,但是我不知道在性能方面是否太昂贵.您可以使用TextWatcher为基于字符颜色映射的上一个字符插入着色.
Yes it's possible, but I don't know if is too expensive in terms of performance. You can use a TextWatcher to color you last char inserted base on your Map of char-color.
public class MainActivity extends AppCompatActivity {
private SpannableStringBuilder mSpannableStringBuilder;
private EditText mEditText;
private static final Map<String, Integer> COLORS_MAP = new HashMap<>();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here you can populate your map with colors
COLORS_MAP.put("a", Color.RED);
COLORS_MAP.put("b", Color.GREEN);
COLORS_MAP.put("c", Color.BLUE);
COLORS_MAP.put("d", Color.MAGENTA);
mSpannableStringBuilder = new SpannableStringBuilder();
mEditText = (EditText) findViewById(R.id.editText);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
// unregister and register the listener to avoid infinite loop
mEditText.removeTextChangedListener(this);
int start = s.length() - 1;
String lastChar = s.toString().substring(start);
SpannableString lastSpannableChar = new SpannableString(lastChar);
// pick the color based on the last char
int color = pickColorByChar(lastChar);
// Span to set char color
ForegroundColorSpan fcs = new ForegroundColorSpan(color);
// Set the text color for the last character
lastSpannableChar.setSpan(fcs, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// append the last char to the string builder so you can keep the previous span
mSpannableStringBuilder.append(lastSpannableChar);
mEditText.setText(mSpannableStringBuilder);
mEditText.setSelection(mEditText.getText().length()); //this is to move the cursor position
mEditText.addTextChangedListener(this);
}
}
});
}
public int pickColorByChar(String aChar){
return COLORS_MAP.get(aChar);
}
}
这是结果
这篇关于如何为Android中用edittext编写的每个字符设置不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!