gpt4 book ai didi

Android 编辑文本 : How to apply Foreground color span as you type?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:07 25 4
gpt4 key购买 nike

我正在尝试在您键入文本时将字体颜色应用于 EditText 中的文本。然而,它非常不一致,这意味着有时如果您键入一个空格,该空格之前的文本将恢复为默认的黑色。或者,如果我将光标放在一个单词的中间并开始输入,整个单词都会改变颜色,而不仅仅是我正在输入的文本。不过,粗体、斜体和下划线似乎效果很好。我如何保证只有我输入的文本会受到字体颜色的影响?

请参阅下面的“尺寸和颜色”评论...

     contentEdit.addTextChangedListener(new TextWatcher() { 
public void afterTextChanged(Editable s) {

//add style as the user types if a toggle button is enabled
ToggleButton boldButton = (ToggleButton) findViewById(R.id.bold);
ToggleButton emButton = (ToggleButton) findViewById(R.id.italic);
ToggleButton underlineButton = (ToggleButton) findViewById(R.id.underline);

int position = Selection.getSelectionStart(contentEdit.getText());

try{
if (position < 0){
position = 0;
}

if (position > 0){

if (styleStart > position || position > (cursorLoc + 1)){
//user changed cursor location, reset
if (position - cursorLoc > 1){
//user pasted text
styleStart = cursorLoc;
}
else{
styleStart = position - 1;
}
}

if (boldButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);

for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (emButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);

for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.ITALIC){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (underlineButton.isChecked()){
UnderlineSpan[] ss = s.getSpans(styleStart, position, UnderlineSpan.class);

for (int i = 0; i < ss.length; i++) {
s.removeSpan(ss[i]);
}
s.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

//SIZE AND COLOR//////////////////////////////////////////////////////
s.setSpan(new ForegroundColorSpan(m_color), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
s.setSpan(new AbsoluteSizeSpan(m_curSize, true), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
catch(Exception e){
//Toast.makeText(m_ctx, m_ctx.gets, Toast.LENGTH_LONG).show();
showMessage(R.string.NOTE_WARNING_STYLE,m_utils.MSGTYPE_WARNING);
}

cursorLoc = Selection.getSelectionStart(contentEdit.getText());
}

最佳答案

您可以使用模式来查找单词,然后将任何跨度应用于单词。

@Override
public void afterTextChanged(Editable s) {
Spannable textSpan = s;
final Pattern pattern = Pattern.compile("\\w+");
final Matcher matcher = pattern.matcher(textSpan);
while (matcher.find()) {
start = matcher.start();
end = matcher.end();
textSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

}

就是这样。它将突出显示您输入的所有匹配词。希望对您有所帮助。

关于Android 编辑文本 : How to apply Foreground color span as you type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18327817/

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