我正在尝试在 android 中实现“Rich Edittext”,我想应用“粗体、斜体、下划线、项目符号跨度、数字缩进跨度等...”效果。因此,不知何故我已经完成了粗体、斜体和其他功能,但我无法完成 Bullet 和 NumberIndent。
我做了很多研究并检查了很多示例和其他内容,但无法完成任务。我从下面的链接下载了一些样式类,但我无法理解如何在我的代码中使用此类。
所以这里的任何人都做过类似的任务或任何其他引导我走向正确方向的人。
最佳答案
public class TextTypeOption {
public final static int BOLD = 1;
public final static int ITALIC = 2;
public final static int UNDERLINE = 3;
public TextTypeOption(int option) {
this.option = option;
}
public int getOption() {
return this.option
}
public boolean isBold() {
return option == BOLD;
}
public boolean isItalic() {
return option == ITALIC;
}
public boolean isUnderline() {
return option == UNDERLINE;
}
}
定义一个 TextTypeOption 值:
TextTypeOption option = new TextTypeOption(TextTypeOption.BOLD);//for bold
1) 使用 Html.fromText(String text)
if(option.isBold()) {
text = text.replace(selectedText , "<b>"+ (selectedText + "</b>");
et.setText(Html.fromHtml(text));
}
if(option.isItalic()) {
text = text.replace(selectedText , "<i>"+ (selectedText + "</i>");
et.setText(Html.fromHtml(text));
}
if(option.isUnderline()) {
text = text.replace(selectedText , "<u>"+ (selectedText + "</u>");
et.setText(Html.fromHtml(text));
}
2) 使用Spannable
SpannableString span = new SpannableString(text);
if(option.isUnderline()) {
span.setSpan(new UnderlineSpan(),startSelection, endSelection , 0);
}
if(option.isBold()) {
span.setSpan(new StyleSpan(Typeface.BOLD), startSelection, endSelection , 0);
}
if(option.isItalic()) {
span.setSpan(new StyleSpan(Typeface.ITALIC), startSelection, endSelection , 0);
}
et.setText(span, TextView.BufferType.SPANNABLE);
附言我认为这不是优雅的方式,但它确实有效。
关于android - 如何在android中实现 "Rich Edittext",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27798227/