如何在Android中使EditText加粗、斜体和下划线

3
我正在使用一个EditText来支持加粗,斜体和下划线的属性。当我选择文本并点击按钮加粗后,我成功了。
现在,我想在选择文本并单击加粗按钮之后再次删除加粗。
它简单地没有设置标志以进行加粗和取消加粗。我必须知道我们选择的文本是否为粗体,如果是粗体,则必须通过单击相同的按钮来删除粗体。需要此文本编辑器支持版本2.3及以上。
非常感谢任何帮助。谢谢:)
1个回答

1
以下代码提供了了解如何扩展EditText类的入门。此代码的作用是在每行单词下面画一条线。但这并不提供rtf格式。
public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        // set your own color here, referencing color resource file
        int color = ContextCompat.getColor(context, R.color.edit_note_line);
        mPaint.setColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();//for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);//first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接