我正在创建一个富文本编辑器。要求是,用户将在编辑文本中使用编辑器。一旦用户完成对文本的编辑,编辑文本的html内容将被收集并发布到服务器。 edittexts'样式应该发布到服务器。
我已在富文本编辑器edittext中实现了格式化,如下所示:
mainEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable spanTest) {
boldBtn = (ToggleButton) findViewById(R.id.boldBtn);
italicBtn = (ToggleButton) findViewById(R.id.italicBtn);
underLineBtn = (ToggleButton) findViewById(R.id.underLineBtn);
// http://developer.android.com/reference/android/text/Selection.html
// Runa toast for the selector index
// http://developer.android.com/reference/android/content/ClipboardManager.html
try {
int selectionStart = Math.max(mainEditText.getSelectionStart(), 0);
int position = Selection.getSelectionStart(mainEditText.getText());
if (position < 0) {
position = 0;
}
if (position > 0) {
if (selectionStart > position || position > (cursorLoc + 1)) {
// user changed cursor location, reset
selectionStart = position - 1;
}
cursorLoc = position;
if (boldBtn.isChecked()) {
// edited to make texteditor bug free
if (cursorLoc >= styleStart) {
spanTest.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.e("Html span: " , Html.toHtml(spanTest));
} else {}
}
if (italicBtn.isChecked()) {
// edited to make texteditor bug free
if (cursorLoc >= styleStart) {
spanTest.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {}
}
if (underLineBtn.isChecked()) {
//edited to make texteditor bug free
if (cursorLoc >= styleStart) {
spanTest.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {}
}
}
} catch (IndexOutOfBoundsException varName) {}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// testClipboard();
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
int startSelection = Selection.getSelectionStart(s);
int endSelection = Selection.getSelectionEnd(s);
}
});
这是我在将文本发送到服务器之前检查文本的方式:
String htmlString=Html.toHtml(mainEditText.getText());
makeAToast(htmlString);
Log.d("htmlString", htmlString);
生成的html是嵌套标签:
<p dir="ltr">hello boys <b><b><b><b><b><b><b><b><b>n</b></b></b></b></b></b></b></b></b><b><b><b><b><b><b><b><b>o</b></b></b></b></b></b></b></b><b><b><b><b><b><b><b>w</b></b></b></b></b></b></b><b><b><b><b><b><b> </b></b></b></b></b></b><b><b><b><b><b>b</b></b></b></b></b><b><b><b><b>o</b></b></b></b><b><b><b>l</b></b></b><b><b>d</b></b><b> </b></p>
我在哪里做错了?在将其发送到服务器之前,应该调整什么才能生成正确的html?
答案 0 :(得分:0)
由于为同一样式添加了多个StyleSpan
,因此最好检查相同的范围是否已应用于您的选择。
// get existing spans on selection
StyleSpan[] existingSpans = spanTest.getSpans (editText.getSelectionStart(), editText.getSelectionEnd(), StyleSpan.class);
boolean styleExists = false;
int yourStyle = Typeface.BOLD; // add your button logic here to get the appropriate style
foreach (StyleSpan span : existingSpans)
{
if (span.getStyle() == yourStyle)
{
styleExists = true;
break;
}
}
if (!styleExists)
{
spanTest.setSpan (new StyleSpan (yourStyle, editText.getSelectionStart(), editText.getSelectionEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else
{
// do nothing as a span with same style already exists.
}