2011-05-12 54 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); 
    } 
}