繁体   English   中英

如何在Android中实现“ Rich Edittext”

[英]How to implement “Rich Edittext” in android

我正在尝试在Android中实现“ Rich Edittext”,我想应用“粗体,斜体,下划线,项目符号范围,数字缩进范围等...”效果。 因此,以某种方式,我已经完成了粗体,斜体和其他功能,但是我无法完成Bullet和NumberIndent。

我做了很多研究,检查了很多例子和其他东西,但是无法完成任务。 我已经从下面的链接下载了一些样式类,但是我无法理解如何在代码中使用该类。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.7_r1/android/text/style/BulletSpan.java?av=f

因此,在这里执行过类似任务或其他任何引导我朝正确方向前进的人。

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选项=新的TextTypeOption(TextTypeOption.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);

附言:我认为这不是一种优雅的方法,但它确实有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM