安卓原生 Edittext富文本编辑,支持Html格式字符串转换

来源:互联网 发布:买域名一年多少钱 编辑:程序博客网 时间:2024/06/08 18:40

    主要的是监听edittext的addTextChangedListener事件,主要是通过span来改变Edittext字体样式,通过HTML.toHtml方法来吧span转成html格式的字符串。

    content.addTextChangedListener(new TextWatcher() {

        int istart = -1;
        int icount = -1;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Log.e("count", "START:"+start+"/"+count+"");
istart = start;
icount = count;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Log.e("before", "START:"+start+"/"+count+"");
// beforeCount = count;
}

@Override
public void afterTextChanged(Editable s) { 
// content.onEditorAction(EditorInfo.IME_ACTION_UNSPECIFIED);
// Log.e("after", "START:"+istart+"/"+icount+"");
if (istart != -1 && icount != -1 ) {
int iend = istart + icount;
if (b.isChecked()) {
s.setSpan(new StyleSpan(Typeface.BOLD), istart, iend, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}
    if (i.isChecked()) {
    s.setSpan(new StyleSpan(Typeface.ITALIC), istart, iend, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
   
    }
    if (d.isChecked()) {
    s.setSpan(new StrikethroughSpan(), istart, iend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     
    }
    if (u.isChecked()) {
    s.setSpan(new UnderlineSpan(), istart, iend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   
    }
    if (boolImg) { 
       Drawable d= new BitmapDrawable(pic);
       d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
       ImageSpan span = new ImageSpan(d,Comm_Args.UP_FILE_URL+imgurl);
       s.setSpan(span, istart, iend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    boolImg = false;
}
   
    if (frontColor != -1) {
    s.setSpan(new BackgroundColorSpan(frontColor), istart, iend, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//     frontColor = -1;
    }
    if (backColor != -1) {
    s.setSpan(new ForegroundColorSpan(backColor), istart, iend, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//     backColor = -1;
}
    if (size != -1) {
s.setSpan(new AbsoluteSizeSpan(10+size*5,true), istart, iend,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
    istart = -1; icount = -1;
}
data=TextType.removeHtml(Html.toHtml(s));
// Html.f   
((TextView)findViewById(R.id.mabi)).setText(data);
}
});


0 0
原创粉丝点击