我正在尝试在onDraw()
中实施自定义EditText
方法。
onDraw
被调用 - 我可以看到日志消息,但它没有绘制任何东西。
有谁能告诉我我做错了什么?
以下是我的布局摘录:
<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
答案 0 :(得分:2)
我认为你应该尝试这个东西来在Android布局的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" />
希望这会使你的工作。
答案 1 :(得分:2)
paint.setColor(0x80FF0000);
不是
paint.setColor(0xFF0000);
显然,通过排除字母字节,您隐式传入零,这意味着颜色是完全透明的。 Java AWT不是这样工作的 - 谁认为这是个好主意?!