1

我正在嘗試在我的EditText中實現自定義onDraw()方法。 onDraw正在調用 - 我可以看到日誌消息,但它沒有繪製任何東西。自定義Android EditText不能正常工作

任何人都可以告訴我我做錯了什麼?

這裏是我的佈局的摘錄:

<view xmlns:android="http://schemas.android.com/apk/res/android" 
      class ="my.package.NotePadEditView" 
      android:inputType="textMultiLine" 
      android:id="@+id/edit_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="top" 
      android:background="@android:color/transparent" 
      android:singleLine="false" 
      > 
     <requestFocus/> 
    </view> 
</ScrollView> 

這裏是類(只是一些測試代碼現在):

public class NotePadEditView extends EditText { 
Paint paint = new Paint(); 
public NotePadEditView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(3); 
    paint.setColor(0xFF0000); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
    Log.d("NotePadEditView", "Calling onDraw()"); // These log messages are displaying 
    canvas.drawLine(0, 0, 50, 50, paint); // just some random stuff so we know when we are done. (Note: these are not displaying - what's up with that???) 
    canvas.drawText("Hello, World", 30, 30, paint); 
    super.onDraw(canvas); 
    } 

// more constructors, etc 
+1

嘗試先調用'super.onDraw(canvas);'。也許這是繪製在你的'drawLine()'和'drawText()'之上的。 – Macarse 2012-04-11 04:12:04

+0

我試過 - 事實上,我把整個繪圖的東西放在一個方法中,並在super.onDraw之前和之後調用它 - 瘋狂的複雜! – 2012-04-11 10:17:50

回答

2

好的,終於搞明白了。看起來你需要在顏色分配設置alpha字節:

paint.setColor(0x80FF0000); 

paint.setColor(0xFF0000); 

顯然排除阿爾法字節,你是隱式傳遞零,這意味着顏色是完全透明的。 Java AWT不會這樣工作 - 誰認爲這是一個好主意?!

2

我想你應該嘗試這個東西在android Layout的xml中使用Custom EditText。

這是我在你的課堂上所做的一些改變。

public class NotePadEditView extends EditText{ 
@Override 
protected void onDraw(Canvas canvas) { 

    Log.d("NotePadEditView", "Calling onDraw()"); // These log messages are displaying 
     canvas.drawLine(0, 0, 50, 50, paint); // just some random stuff so we know when we are done. (Note: these are not displaying - what's up with that???) 
     canvas.drawText("Hello, World", 30, 30, paint); 
     super.onDraw(canvas); 

} 
Paint paint; 

public NotePadEditView(Context context, AttributeSet attrs){ 
    super(context, attrs); 
    //this Contructure required when you are using this view in xml 
    paint = new Paint(); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(3); 
    paint.setColor(Color.BLUE); 
} 

public NotePadEditView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    paint = new Paint(); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(3); 
    paint.setColor(0xFF0000); 

    } 

} 

使用在這樣的XML,

<my.package.NotePadEditView 
      android:id="@+id/edit_text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" 
      android:gravity="top" 
      android:inputType="textMultiLine" 
      android:singleLine="false" /> 

希望這將使您的工作。

+0

我沒有看到太大的區別 - 我已經在所有構造函數中設置了paint,並且我相信您的xml與我的相同。 – 2012-04-11 10:19:40

+0

但是我在使用你沒有'NotePadEditView(Context context,AttributeSet attrs)'這個參數化構造函數的代碼時得到錯誤。如果我們不在類中添加它,它會在xml layout.and中使用問題我可以在Edittext上看到藍線。 – Herry 2012-04-11 10:26:52

+0

是的,我有那個構造函數,我只是沒有在代碼中顯示它 - 你在底部看到有一個評論說「更多的構造函數」。對困惑感到抱歉。無論如何,它仍然沒有工作 – 2012-04-11 23:21:03