2011-05-12 34 views
6

我正在看记事本样品在Android SDK在这里看到:http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html在edittext中绘制多行记事本

事情是只汲取当前行光标在如http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg

但我想显示的行填满屏幕,例如http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png

任何建议将是伟大的。代码的相关位似乎在这里:

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

回答

2

也许在循环之后,您可以绘制估计的*附加行。

的getHeight()将返回的EditText的高度,以像素 getLineHeight()将一个标准线的高度

这样的getHeight/getlineHeight-getCount将将行数从左绘制。

你不能使用getLineBounds,使用上面的函数可以计算剩下的画线的位置。

*由于文本的格式化可能会改变行高,但由于这些行中没有文本,所以不应该是一个问题。但出于同样的原因,你应该只绘制其余的线,而不是用这个绘制所有的线。

+0

+1很好的解释.... – 2011-06-09 05:59:57

31

这是代码的基础上,jkhouws1的建议和谷歌的note editor

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

在Eclipse IDE中按下Ctrl + Shift + O添加所有需要进口

+0

+1正确实施jkhouw1的答案。按需要工作。 – 2011-06-09 06:00:33

+0

是否可以增加线之间的间距。如果我做了'baseline + = getLineHeight()+ 30',那么行之间会有间隔,但是当我按下ENTER时,光标不在行的上面。它在中间 – 2013-11-19 16:23:33

+0

这对我有帮助。对于放置虚线使用下面的代码:PathEffect effects = new DashPathEffect(new float [] {3,3,3,3},1); \t \t mPaint.setPathEffect(effects);在LinedEditText构造函数中。 – 2014-01-06 06:34:20

3

我觉得这是你所需要的:

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


} 
-1
<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="" 
    /> 

上述XML的工作原理与代码Max4ever's answer

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