4

是否可以在 a 中显示页面行EditText

我的意思是这些行:

在此处输入图像描述

假设我EditText的大小为 500 x 500 像素。我希望这些线条在 500 x 500 平方范围内可见。

有没有办法做到这一点?我已经尝试过谷歌,但我找不到答案。我想我的另一个选择是根据 textheight 和 linepacing 动态创建图形,这是一个丑陋的解决方法。

4

3 回答 3

8

来自 android 开发网站的记事本应用程序示例向您展示了如何执行此操作。

http://developer.android.com/resources/samples/NotePad/index.html

看起来像这样(向下滚动查看代码):

记事本

大部分相关代码都在这个文件中。注意LinedEditText内部类。它在活动中定义。它绘制所需的线条。

在活动onCreate()方法内部,设置为视图setContentView(R.id.note_editor)定义如下

从这里提取的片段。更新:@Pieter888 修改代码以在整个EditText控件上绘制线条。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

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(0xFF000000);
    }

    /**
     * This is called to draw the LinedEditText object
     * @param canvas The canvas on which the background is drawn.
     */
    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = canvas.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);
    }
}
于 2012-06-12T09:18:22.177 回答
1

@gideon 上面的答案效果很好,但是当您在其中输入更多文本时会出现问题,edittext因为没有相应地绘制更多行。为了解决这个问题,我写了一个覆盖onMeasure()并调用invalidate(). 当文本增加时,将绘制更多的线条。

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    invalidate();
  }

现在的代码是:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

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(0xFF000000);
    }

    /**
     * This is called to draw the LinedEditText object
     * @param canvas The canvas on which the background is drawn.
     */
    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = canvas.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);
    }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   super.onMeasure(widthMeasureSpec,heightMeasureSpec);
   invalidate();
      }
}
于 2016-10-01T08:43:10.117 回答
0

@gideon 的代码有效,但未绘制更多行

您必须更改canvas.getHeight()getHeight()如下:

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);
}
于 2019-09-05T11:28:55.100 回答