简体   繁体   English

如何在Android中实现“ Rich Edittext”

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

I am trying to implement "Rich Edittext" in android, i want to apply effect like "Bold, italic, underline, Bullet span, Number Indent span etc..." effect. 我正在尝试在Android中实现“ Rich Edittext”,我想应用“粗体,斜体,下划线,项目符号范围,数字缩进范围等...”效果。 So, Somehow i have completed Bold, Italic and other functionality but i am not able to complete Bullet and NumberIndent. 因此,以某种方式,我已经完成了粗体,斜体和其他功能,但是我无法完成Bullet和NumberIndent。

I did lot's of research and checked so many example and other stuff's but couldn't complete the task. 我做了很多研究,检查了很多例子和其他东西,但是无法完成任务。 I have downloaded some style class from below link but i am not able to understand how can i use this class in my code. 我已经从下面的链接下载了一些样式类,但是我无法理解如何在代码中使用该类。

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

So anyone here who have done some similar task or anything else which lead me to the right direction. 因此,在这里执行过类似任务或其他任何引导我朝正确方向前进的人。

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;
    }
}

Define a TextTypeOption value : 定义一个TextTypeOption值:

TextTypeOption option = new TextTypeOption(TextTypeOption.BOLD);//for bold TextTypeOption选项=新的TextTypeOption(TextTypeOption.BOLD); //为粗体

1) use the Html.fromText(String text) 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) use the Spannable 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);

PS I think it's not the elegant way but it works. 附言:我认为这不是一种优雅的方法,但它确实有效。

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

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