如何知道开放平台添加到Android上的EditText?(How to add pagelines

2019-06-25 10:16发布

是否有可能显示一个知道开放平台EditText

我的意思是这几行:

比方说,我EditText在尺寸500由500像素。 我希望这些线由500平方米是跨500看到。

是否有办法构建做到这一点? 我已经尝试过谷歌,但我无法找到答案。 我想我的另一种选择是动态地创建基于textHeight不同和linespacing,这种丑陋的变通的图形。

Answer 1:

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


Answer 2:

@以上基甸的回答运作良好,但是当你进入的更多的文本有问题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();
      }
}


文章来源: How to add pagelines to a EditText in android?