How to add pagelines to a EditText in android?

后端 未结 3 1877
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 14:07

Is it possible to show pagelines in a EditText?

I mean these lines:

\"enter

3条回答
  •  悲哀的现实
    2021-01-14 14:23

    @gideon's code works but more lines not be drawn

    you must just change canvas.getHeight() to getHeight() as following below:

    public class LinedEditText extends EditText
     {
    private Rect mRect;
    private Paint mPaint;
    
    public LinedEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(ResourcesCompat.getColor(getResources(), R.color.blue,null));
    }
    
    
    @Override
    protected void onDraw(Canvas canvas)
    {
        int height = getHeight();
        int curHeight = 0;
        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);
        for (curHeight = baseline + 1; curHeight < height;
             curHeight += getLineHeight())
        {
            canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
        }
        super.onDraw(canvas);
    }
    

提交回复
热议问题