Android 在编辑文本(如记事本)中绘制多行

Android 在编辑文本(如记事本)中绘制多行,android,android-edittext,Android,Android Edittext,我正在查看android SDK中的记事本示例,请参见: 问题是它只画光标所在的当前行 但是我想显示填充屏幕的行,例如 任何建议都很好。相关代码如下所示: protected void onDraw(Canvas canvas) { // Gets the number of lines of text in the View. int count = getLineCount(); // Gets the global Rect an

我正在查看android SDK中的记事本示例,请参见:

问题是它只画光标所在的当前行

但是我想显示填充屏幕的行,例如

任何建议都很好。相关代码如下所示:

    protected void onDraw(Canvas canvas) {

        // Gets the number of lines of text in the View.
        int count = getLineCount();

        // Gets the global Rect and Paint objects
        Rect r = mRect;
        Paint paint = mPaint;

        /*
         * Draws one line in the rectangle for every line of text in the EditText
         */
        for (int i = 0; i < count; i++) {

            // Gets the baseline coordinates for the current line of text
            int baseline = getLineBounds(i, r);

            /*
             * Draws a line in the background from the left of the rectangle to the right,
             * at a vertical position one dip below the baseline, using the "paint" object
             * for details.
             */
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        // Finishes up by calling the parent method
        super.onDraw(canvas);
    }
受保护的void onDraw(画布){
//获取视图中的文本行数。
int count=getLineCount();
//获取全局Rect和Paint对象
Rect r=mRect;
油漆油漆=mPaint;
/*
*为EditText中的每行文本在矩形中绘制一行
*/
for(int i=0;i
也许在for循环之后,您可以绘制估计的*额外线条

getHeight()将以像素为单位返回EditText的高度 getLineHeight()将显示一条标准线的高度

所以getHeight/getlineHeight getCount将是要绘制的剩余线条数

不能使用getLineBounds,使用上述函数可以计算要绘制的其余直线的位置


*估计,因为文本格式可能会改变行高,但由于这些行中还没有文本,所以这不应该是一个问题。但是出于同样的原因,你应该只画剩下的几行,而不是用它来画所有的行。

这是代码,基于的建议和谷歌的建议

公共类LinedEditText扩展了EditText{
私人直肠;
私人油漆;
//我们需要这个构造器来实现LayoutFlater
public LinedEditText(上下文、属性集属性){
超级(上下文,attrs);
mRect=新的Rect();
mPaint=新油漆();
mPaint.setStyle(绘制、样式、填充和笔划);
mPaint.setColor(R.color.edit_note_line);//在此处设置您自己的颜色
}
@凌驾
受保护的void onDraw(画布){
//int count=getLineCount();
int height=getHeight();
int line_height=getLineHeight();
int count=高度/线条高度;
如果(getLineCount()>count)
count=getLineCount();//用于带滚动的长文本
Rect r=mRect;
油漆油漆=mPaint;
int baseline=getLineBounds(0,r);//第一行
for(int i=0;i

在Eclipse IDE中,按Ctrl+Shift+O添加所有需要的导入

我认为这就是您需要的:

public class LinedEditText extends EditText {

    private static Paint linePaint;

    static {
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Style.STROKE);
    }

    public LinedEditText(Context context, AttributeSet attributes) {
        super(context, attributes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect bounds = new Rect();
        int firstLineY = getLineBounds(0, bounds);
        int lineHeight = getLineHeight();
        int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);

        for (int i = 0; i < totalLines; i++) {
            int lineY = firstLineY + i * lineHeight;
            canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
        }

        super.onDraw(canvas);
    }


}
公共类LinedEditText扩展了EditText{
私人静电漆;
静止的{
linePaint=新油漆();
linePaint.setColor(颜色:黑色);
linePaint.setStyle(样式笔划);
}
public linededitext(上下文上下文、属性集属性){
超级(上下文、属性);
}
@凌驾
受保护的void onDraw(画布){
Rect bounds=new Rect();
int firstLineY=getLineBounds(0,bounds);
int lineHeight=getLineHeight();
int totalines=Math.max(getLineCount(),getHeight()/lineHeight);
对于(int i=0;i

上述XML可用于以下代码:

公共类LinedEditText扩展了EditText{
私人直肠;
私人油漆;
//我们需要这个构造器来实现LayoutFlater
public LinedEditText(上下文、属性集属性){
超级(上下文,attrs);
mRect=新的Rect();
mPaint=新油漆();
mPaint.setStyle(绘制、样式、填充和笔划);
mPaint.setColor(R.color.edit_note_line);//在此处设置您自己的颜色
}
@凌驾
受保护的void onDraw(画布){
//int count=getLineCount();
int height=getHeight();
int line_height=getLineHeight();
int count=高度/线条高度;
如果(getLineCount()>count)
count=getLineCount();//用于带滚动的长文本
Rect r=mRect;
油漆油漆=mPaint;
int baseline=getLineBounds(0,r);//第一行
for(int i=0;i
是否可以增加行间距。如果我执行
baseline+=getLineHeight()+30
,则行与行之间存在间距,但当我按ENTER键时,光标不在行上方。它在中间,这对我有帮助。要放置点线,请使用以下代码:PathEffect effects=new DashPathEffect(new float[]{3,3,3},1);mPaint.setPathEffect(效果);InlineDeditText构造函数。首先,非常感谢您提供了友好且简单的解决方案。我真的很感激。我可以在行首也显示行号吗。还有一件事我想问,如果我们添加行号,那么它将成为edittext文本的一部分,或者它将被视为占位符文本,这样我们可以看到它,但不能复制它。请分享你对此的美好想法。谢谢
public class LinedEditText extends EditText {

    private static Paint linePaint;

    static {
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Style.STROKE);
    }

    public LinedEditText(Context context, AttributeSet attributes) {
        super(context, attributes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect bounds = new Rect();
        int firstLineY = getLineBounds(0, bounds);
        int lineHeight = getLineHeight();
        int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);

        for (int i = 0; i < totalLines; i++) {
            int lineY = firstLineY + i * lineHeight;
            canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
        }

        super.onDraw(canvas);
    }


}
<com.example.goh2.pronoornotepad.LinedEditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffcc4b"
        android:gravity="top|left"
        android:singleLine="false"
        android:text=""
     />
   public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

// we need this constructor for LayoutInflater
   public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
    }
 }