如何在android编辑文本中应用Bullet和编号列表

时间:2015-01-12 05:47:56

标签: android spannable richeditabletext bullet-span

我正在尝试在用户按下按钮时在android编辑文本中应用项目符号列表和编号列表。为此,我尝试了下面的代码。

对于子弹

BulletSpan[] quoteSpan = str.getSpans(selectionStart, selectionEnd, BulletSpan.class);

                // If the selected text-part already has UNDERLINE style on it, then we need to disable it
                for (int i = 0; i < quoteSpan.length; i++) {
                    str.removeSpan(quoteSpan[i]);
                    exists = true;
                }

                // Else we set UNDERLINE style on it
                if (!exists) {
                    str.setSpan(new BulletSpan(10), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                break;

和编号

int number = 0;
                NumberIndentSpan[] quoteSpan1 = str.getSpans(selectionStart, selectionEnd, NumberIndentSpan.class);
                number ++;
                // If the selected text-part already has UNDERLINE style on it, then we need to disable it
                for (int i = 0; i < quoteSpan1.length; i++) {
                    str.removeSpan(quoteSpan1[i]);
                    exists = true;
                }

                if (!exists){
                    str.setSpan(new NumberIndentSpan(5, 15, number), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                break;

NumberIndentSpan.java

public class NumberIndentSpan implements LeadingMarginSpan {

    private final int gapWidth;
    private final int leadWidth;
    private final int index;

    public NumberIndentSpan(int leadGap, int gapWidth, int index) {
        this.leadWidth = leadGap;
        this.gapWidth = gapWidth;
        this.index = index;
    }

    public int getLeadingMargin(boolean first) {
        return leadWidth + gapWidth;
    }

    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top,
            int baseline, int bottom, CharSequence text, int start, int end,
            boolean first, Layout l) {
        if (first) {
            Paint.Style orgStyle = p.getStyle();
            p.setStyle(Paint.Style.FILL);
            float width = p.measureText("4.");
            c.drawText(index + ")", (leadWidth + x - width / 2) * dir, bottom
                    - p.descent(), p);
            p.setStyle(orgStyle);
        }
    }
}

通过上面的代码,我能够在特定的位置添加Bullet和Number,但问题是,我想实现这样的功能,当我按下回车键时,它应该自动添加Bullet到下一行,如果我按Enter键没有如果我在第二行输入一些文本,然后如果我按下回车键,则输入任何文本它应删除该子弹,它也应该将Bullet添加到下一行。像这个应用程序类似的功能。 https://play.google.com/store/apps/details?id=com.hly.notes

同样的数字列表,它总是添加 1)它没有递增值,对于数字列表我也想实现类似的功能之王像bullet,当我应用数字列表当我按下回车键时,它应自动将 2)添加到下一行。

任何人都知道如何实现这一目标。先感谢您。

0 个答案:

没有答案