2012-07-17 43 views
1

我想做一個音符編輯器。它將有一個帶有線條的EditText。這裏是我的代碼:EditText畫線錯誤

LinedEditText.java 

public class LinedEditText extends EditText { 


    private Rect mRect; 
    private Paint mPaint; 

    public LinedEditText(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     mRect = new Rect(); 
     mPaint = new Paint(); 
     mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     mPaint.setColor(Color.BLUE); //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(); 

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

TextEditorActivity.java 

    public class TextEditorActivity extends Activity { 

    private LinedEditText text; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     this.text=new LinedEditText(getApplicationContext()); 
     this.setContentView(text); 
    } 

} 

而且結果:

result http://bekirmavus.com/k-resimler/image/android/device-2012-07-17-222950.png

我在做什麼錯誤?

謝謝...

回答

3

該行正在從光標位置向下繪製。只要移動光標的默認位置從垂直居中,在左上角,用重力:

text.setGravity(Gravity.NO_GRAVITY); 

或將其設爲默認爲所有LinedEditTexts:

public LinedEditTexts(Context context) { 
    super(context); 
    setGravity(Gravity.NO_GRAVITY); // or Gravity.TOP | Gravity.LEFT 
    ... 
} 
+0

對不起,我不能早期測試。 謝謝。 – bmavus 2012-07-18 17:03:13