Android版的EditText:如何申请前景色跨度为您键入? [英] Android EditText : How to apply Foreground color span as you type?
问题描述
我想申请一个字体颜色为您键入的文本在一个EditText文本。然而,它只是很不一致,这意味着有时,如果你键入一个空格,文本preceding空间会回到默认的黑色。或者,如果我把我的光标在字的中间,开始输入整个单词会改变颜色,不只是我打字的文字。该粗体,斜体和下划线看起来虽然工作得很好。我怎么能保证只有我打字文本将与问候字体颜色会受到影响?
请参阅大小和颜色下面的评论...
contentEdit.addTextChangedListener(新TextWatcher(){
公共无效afterTextChanged(编辑S){ //如果切换按钮启用添加样式为用户类型
切换按钮boldButton =(切换按钮)findViewById(R.id.bold);
切换按钮emButton =(切换按钮)findViewById(R.id.italic);
切换按钮underlineButton =(切换按钮)findViewById(R.id.underline); INT位置= Selection.getSelectionStart(contentEdit.getText()); 尝试{
如果(位置℃,){
位置= 0;
} 如果(位置大于0){ 如果(styleStart>仓位||位置>(cursorLoc + 1)){
//用户改变光标位置,复位
如果(位置 - cursorLoc→1){
//用户粘贴的文本
styleStart = cursorLoc;
}
其他{
styleStart =位置 - 1;
}
} 如果(boldButton.isChecked()){
StyleSpan [] SS = s.getSpans(styleStart,位置StyleSpan.class); 的for(int i = 0; I< ss.length;我++){
如果(SS [I] .getStyle()== android.graphics.Typeface.BOLD){
s.removeSpan(SS [I]);
}
}
s.setSpan(新StyleSpan(android.graphics.Typeface.BOLD),styleStart,位置,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
如果(emButton.isChecked()){
StyleSpan [] SS = s.getSpans(styleStart,位置StyleSpan.class); 的for(int i = 0; I< ss.length;我++){
如果(SS [I] .getStyle()== android.graphics.Typeface.ITALIC){
s.removeSpan(SS [I]);
}
}
s.setSpan(新StyleSpan(android.graphics.Typeface.ITALIC),styleStart,位置,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
如果(underlineButton.isChecked()){
UnderlineSpan [] SS = s.getSpans(styleStart,位置UnderlineSpan.class); 的for(int i = 0; I< ss.length;我++){
s.removeSpan(SS [I]);
}
s.setSpan(新UnderlineSpan(),styleStart,位置,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} //大小和颜色///////////////////////////////////////////// /////////
s.setSpan(新ForegroundColorSpan(m_color),位置,位置,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
s.setSpan(新AbsoluteSizeSpan(m_curSize,真),位置,位置,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
赶上(例外五){
//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());
}
您可以使用模式查找单词,然后应用任何跨度的话。
@覆盖
公共无效afterTextChanged(编辑S){
Spannable textSpan =秒;
最终的模式模式= Pattern.compile(\\\\ W +);
最终匹配器匹配= pattern.matcher(textSpan);
而(matcher.find()){
开始= matcher.start();
结束= matcher.end();
textSpan.setSpan(新ForegroundColorSpan(getResources()的getColor(R.color.red)),开始,结束,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
就是这样。它会突出显示所有匹配的话,你打字。
希望它帮助。
I'm trying to apply a font color to text in an EditText as you type text. However, its just very inconsistent, meaning that sometimes if you type a space, the text preceding that space will go back to the default black color. Or if I put my cursor in the middle of a word and start typing the entire word changes color and not just the text I'm typing. The bold, italic and underline seem to work well though. How can I guarantee that only the text I'm typing will be affected with regards to font color?
See "SIZE AND COLOR" comment below...
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());
}
You can use pattern for finding words and then apply any span to words.
@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);
}
That's it. it will highlight all matching words you typing. Hope it help.
这篇关于Android版的EditText:如何申请前景色跨度为您键入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!