[英]html converted bold or italic spanned string in android
I have method in my custom EditText that makes the text bold or italic 我的自定义EditText中有使文本变为粗体或斜体的方法
here is my method: 这是我的方法:
public boolean changeTextStyle(TextStyle style) {
if (isTextSelected()) {
int startSelection = getSelectionStart();
int endSelection = getSelectionEnd();
SpannableString span = new SpannableString(getText().subSequence(startSelection, endSelection));
span.setSpan(new StyleSpan(Typeface.NORMAL), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new CustomTypefaceSpan(getTypeface()), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
switch (style) {
case BOLD_STYLE: {
span.setSpan(new StyleSpan(Typeface.BOLD), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
break;
case ITALIC_STYLE: {
span.setSpan(new StyleSpan(Typeface.ITALIC), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
break;
}
getText().replace(startSelection, endSelection, span);
setSelection(endSelection);
return true;
} else {
return false;
}
}
When i switch bold text style to italic style or inverse every thing is fine and in the EditText it change from bold to italic but when i want to convert that text to html the problem comes and returns an html that contains both html tags bolded italic text but i want just switch between bold or italic. 当我将粗体文本样式切换为斜体样式或逆序时,一切都很好,并且在EditText中,它从粗体变为斜体,但是当我想将该文本转换为html时,问题就来了,并返回了一个包含两个html标签的html粗体斜体文本但我只想在粗体或斜体之间切换。
here is my converting to html method: 这是我转换为html方法:
public String getFinalText() {
return StringEscapeUtils.unescapeHtml4(Html.toHtml(getText())
.replace("<p dir=\"ltr\">", "")
.replace("<p dir=\"rtl\">", "")
.replace("<u>", "")
.replace("</u>", "")
.replace("<p>", "")
.replace("</p>", ""))
.trim();
}
in a short way 在短期内
i want this: 我要这个:
This is <b>simple</b> text
or 要么
This is <i>simple</i> text
but the output is this: 但是输出是这样的:
This is <b><i>simple</i></b> text
try to change these lines 尝试改变这些行
SpannableString span = new SpannableString(getText().subSequence(startSelection, endSelection));
span.setSpan(new StyleSpan(Typeface.NORMAL), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
to 至
SpannableString span = new SpannableString(getText().subSequence(startSelection, endSelection));
final StyleSpan[] spans = span.getSpans(0, span.length(), StyleSpan.class);
for (StyleSpan styleSpan : spans) span.removeSpan(styleSpan);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.