2011-05-12 15 views
6

मैं एंड्रॉयड एसडीके में नोटपैड नमूना पर एक नज़र ले रहा था यहाँ देखें: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.htmledittext में एकाधिक रेखाएं खींचना उदा। नोटपैड

बात है यह केवल वर्तमान पंक्ति कर्सर जैसे 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() पिक्सल getLineHeight (में EditText की ऊंचाई वापस आ जाएगी) एक मानक लाइन की ऊंचाई

तो getHeight/getlineHeight-getCount लाइनों की संख्या आकर्षित करने के लिए छोड़ दिया होगा।

आप उपर्युक्त कार्यों का उपयोग करके GetLineBounds का उपयोग नहीं कर सकते हैं, आप शेष पंक्तियों की स्थिति को गणना करने के लिए गणना कर सकते हैं।

* टेक्स्ट की स्वरूपण के बाद अनुमानित रेखा ऊंचाई बदल सकती है, लेकिन चूंकि इन पंक्तियों में कोई टेक्स्ट नहीं है, फिर भी यह कोई मुद्दा नहीं होना चाहिए। लेकिन इसी कारण से आपको केवल शेष रेखाएं खींचना चाहिए, और सभी लाइनों को आकर्षित करने के लिए इसका उपयोग नहीं करना चाहिए।

+0

+1 अच्छा स्पष्टीकरण .... –

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

ग्रहण आईडीई प्रेस में Ctrl + Shift + हे सभी आवश्यक आयात

+0

+1 jkhouw1 का जवाब सही ढंग से लागू। वांछित के रूप में काम करना। –

+0

क्या लाइनों के बीच अंतर को बढ़ाना संभव है। यदि मैं 'बेसलाइन + = getLineHeight() + 30' करता हूं, तो रेखा के बीच अंतर होता है लेकिन जब मैं ENTER दबाता हूं तो कर्सर लाइन से ऊपर नहीं होता है। यह बीच में है –

+0

यह मेरी मदद करता है। डॉट लाइन लाइन को निम्नलिखित कोड का उपयोग करने के लिए: पथ प्रभाव प्रभाव = नया डैशपाथ प्रभाव (नया फ्लोट [] {3, 3, 3, 3}, 1); \t \t mPaint.setPathEffect (प्रभाव); LinedEditText कन्स्ट्रक्टर में। –

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

ऊपर एक्सएमएल 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); 
    } 
}