【问题标题】:How to make EditText bold italic underline android如何使EditText粗斜体下划线android
【发布时间】:2013-11-06 19:22:04
【问题描述】:

我正在使用EditText 来支持粗体、斜体和下划线的属性。选择文本并单击按钮以加粗文本后,我成功了。

现在我想在选择文本并单击粗体按钮后再次删除粗体。

这根本不是将标志设置为粗体并删除粗体。我必须知道我们选择的文本是粗体的,如果它是粗体,那么我们必须通过单击相同的按钮来删除粗体。需要这个文本编辑器从 2.3 版本开始支持。

任何帮助将不胜感激。谢谢:)

【问题讨论】:

    标签: android android-edittext bold underline italic


    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2010-11-25
      • 2021-08-02
      • 1970-01-01
      • 2011-03-03
      相关资源
      最近更新 更多