简体   繁体   English

如何在Android中的EditText中添加页面线?

[英]How to add pagelines to a EditText in android?

Is it possible to show pagelines in a EditText ? 是否可以在EditText显示页面线?

I mean these lines: 我的意思是这些线:

在此输入图像描述

Let's say my EditText is 500 by 500 pixels in size. 假设我的EditText大小为500 x 500像素。 I want those lines to be visible across that 500 by 500 square. 我希望这些线在500×500平方米的范围内可见。

Is there a build in way to do this? 有没有建立方式来做到这一点? I already tried Google but I couldn't find an answer. 我已经尝试了谷歌,但我找不到答案。 I guess my other option is to dynamically create a graphic based on the textheight and linespacing, such an ugly work-around. 我想我的另一个选择是动态创建一个基于textheight和linespacing的图形,这样一个丑陋的解决方法。

The notepad application sample from the android dev site shows you how to do this. 来自android dev网站的记事本应用程序示例向您展示了如何执行此操作。

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

Looks like this (scroll down for code): 看起来像这样(向下滚动代码):

记事本

Most of the relevant code is in this file . 大多数相关代码都在此文件中 Pay attention to the LinedEditText inner class. 注意LinedEditText内部类。 It is defined within the activity. 它在活动中定义。 It draws the lines required. 它绘制了所需的线条。

Inside the activity onCreate() method, setContentView(R.id.note_editor) is set as the view, it is defined like here 在活动onCreate()方法内部, setContentView(R.id.note_editor)被设置为视图,它被定义为这里

Snippet extracted from here . 这里提取的片段。 Update : Code modified by @Pieter888 to draw lines on the entire EditText control. 更新 :@ 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);
    }
}

@gideon's answer above works well but has an issue when you enter more text in the edittext because more lines are not drawn accordingly. @ gideon上面的答案效果很好,但是当您在edittext输入更多文本时会出现问题,因为没有相应地绘制更多行。 To solve this, I wrote an override for onMeasure() and called invalidate() . 为了解决这个问题,我为onMeasure()编写了一个覆盖,并调用了invalidate() and more lines will be drawn when text increases. 文本增加时将绘制更多行。

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

The code now is: 现在的代码是:

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

@gideon's code works but more lines not be drawn @ gideon的代码可以工作,但不能绘制更多的行

you must just change canvas.getHeight() to getHeight() as following below: 你必须将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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM