【问题标题】:How to add pagelines to a EditText in android?如何将分页线添加到 android 中的 EditText?
【发布时间】:2012-06-15 02:01:44
【问题描述】:

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

我的意思是这几行:

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

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

【问题讨论】:

  • 您在编辑文本中需要像记事本一样的行对吗? (不是像背景一样的记事本图形?)
  • 就像这样:我有一个记事本的图形,就像我的示例中的那个,只是没有线条。如果我将线条放在图形本身中,线条将永远不会与文本完全同步,因为图像会缩放以适合屏幕高度(字体大小应该保持不变)。这就是为什么我不想通过使用背景来解决这个问题,而是使用 EditText 本身。
  • 那我的回答应该对你有用:)

标签: android android-layout android-edittext underline


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这太老了……有趣的是它是如何被挖出来的。没有完全考虑过这种情况,我现在离安卓还很远 :) 你的更新做得很好! :) +1
    【解决方案3】:

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

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

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

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

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

    here 中提取的片段。 更新:@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);
        }
    }
    

    【讨论】:

    • 这对我来说效果很好,我目前正在编辑 LinedEditText 以在空行上显示多行(所以它会填满屏幕)
    • 非常感谢!如果您使用此代码更新您的答案:pastebin.com/sJBJtxbR 我可以接受您的答案。我在代码中做了一个小的编辑,使这些行在整个LinedEditText-view 中重复。
    • @Pieter888 已更新 :) 您也可以随时将自己的发现作为突出显示的更新添加到您的问题中。干杯!
    • 它没有显示更多的行......从某种意义上说,如果我在某种程度上输入更多的文本,它没有显示行但允许输入文本
    • 这里是Google's NotePad Sample 的更新链接。检查第 78 行的 LinedEditText
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多