Android的 - 比一的EditText单词不能更大胆 [英] Android - Can't bold more than a word in edittext
问题描述
我有,我想大胆的,我选择文本的EDITTEXT。
我使用的上下文操作栏用按钮来大胆选定的词。
现在的问题是,如果我大胆的一句话我不能大胆其他onesm,如果我从Word中删除的跨度,我不能再添加它。
等是在我写的EDITTEXT,我也用2 SpannableString赶上剩余的文本保持它前加入最终的跨度。
I have an editText in which i want to bold the text that i select. I'm using the Contextual Action Bar with a button to bold a selected word. The problem is that if I bold a word I can't bold the other onesm and if i remove the span from that word, I can't add it again. et is the editText in which i write, and i use also 2 SpannableString to catch the remaining text keeping eventual spans added before on it.
定制的回调
cs1 = new StyleSpan(Typeface.BOLD);
class CustomCallback implements ActionMode.Callback {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//exploiting the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
menu.removeItem(android.R.id.selectAll);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
操作添加粗体
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int start = et.getSelectionStart();
int end = et.getSelectionEnd();
SpannableStringBuilder s_before,s_next;
SpannableStringBuilder ssb = new SpannableStringBuilder(et.getText().subSequence(start, end));
s_before= new SpannableStringBuilder (et.getText().subSequence(0, start));
s_next= new SpannableStringBuilder (et.getText().subSequence(end, et.length()));
switch(item.getItemId()) {
case R.id.bold:
int a=ssb.getSpanStart(cs1);
int b=ssb.getSpanEnd(cs1);
if(a==-1 && b==-1){
ssb.setSpan(cs1, 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else{
ssb.removeSpan(cs1);
}
et.setText("");
et.append(s_before);
et.append(ssb);
et.append(s_next);
return true;
我该如何解决呢?
先谢谢了。
How can i solve it? Thanks in advance.
推荐答案
我刚刚把RichEditText库的https: //github.com/kemallette/RichEditText 这增加粗体/斜体/罢工/强调..和一些其他的字体样式功能。它还增加了一个伟大的一堆验证的。
I've just put a RichEditText library https://github.com/kemallette/RichEditText which adds bold/italic/strike/underline.. and a few other font styles functionalities. It also adds a bunch of great validations.
如果不适合你的需求,你需要把重点放在保持跟踪您的跨度,它们是什么类型,以及他们正在调整/重新添加。采取上述图书馆一看RichEditText和RichTextWatcher类。它会给你的文字时,在你的EditText改变什么实际发生的一个更好的主意。
If that doesn't fit your needs, you'll need to focus on keeping track of where your spans are, what type they are and that they're being adjusted/re-added. Take a look at the RichEditText and RichTextWatcher classes in the above library. It'll give you a better idea of what's actually happening when text changes in your EditText.
这篇关于Android的 - 比一的EditText单词不能更大胆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!