【问题标题】:Custom Android EditText not working自定义 Android EditText 不起作用
【发布时间】:2012-04-11 00:39:12
【问题描述】:

我正在尝试在我的 EditText 中实现自定义 onDraw() 方法。 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

【问题讨论】:

  • 尝试先致电super.onDraw(canvas);。也许那是在您的drawLine()drawText() 之上。
  • 我试过了——事实上我把整个绘图的东西放在一个方法中,并在 super.onDraw 之前和之后调用它——疯狂的困惑!

标签: android android-edittext android-view android-custom-view


【解决方案1】:

我认为你应该试试这个东西在 android 布局的 xml 中使用自定义 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" />

希望这会让你的工作。

【讨论】:

  • 我看不出有什么不同 - 我已经在所有构造函数中设置了绘制,我相信你的 xml 和我的一样。
  • 但是我在使用你没有NotePadEditView(Context context, AttributeSet attrs)这个参数化构造函数的代码时遇到错误。如果我们不在类中添加它,它将在xml布局中使用时产生问题。并且我可以在 Edittext 上看到蓝线。
  • 是的,我有那个构造函数,我只是没有在代码中显示它——你会在底部看到一条注释,上面写着“更多构造函数”。对困惑感到抱歉。无论如何,它仍然无法正常工作
【解决方案2】:

好的,终于明白了。看起来您需要在颜色分配上设置 alpha 字节:

paint.setColor(0x80FF0000); 

不是

paint.setColor(0xFF0000);

显然,通过排除 alpha 字节,您隐式传入零,这意味着颜色是完全透明的。 Java AWT 不是这样工作的——谁认为这是个好主意?!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多