简体   繁体   English

无法在Android画布中绘制editText

[英]Can't draw editText in android canvas

I am trying to add an editText box to my android project. 我正在尝试向我的android项目添加一个editText框。 This project is done almost entirely with Java (barely any xml), so I would like to know how to get this done in Java. 这个项目几乎完全用Java完成(几乎没有XML),所以我想知道如何用Java完成这项工作。 My current implementation which is getting a runtime error is as follows: 我当前遇到运行时错误的实现如下:

public class MainMenu extends View{
    EditText editText;

    public MainMenu(Context context) {
        super(context);
        EditText editText = new EditText(context);
        editText.setDrawingCacheEnabled(true);
        editText.setText("My Text");
        editText.setWidth(180);         
        editText.setBackgroundColor(Color.WHITE);
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        editText.draw(canvas);
        invalidate();
    }
}

Can anyone point out what is wrong and possibly offer a solution using Java? 谁能指出有什么问题,并可能提供使用Java的解决方案?

EditText editText = new EditText(context);

Your "editText" will be null in the onDraw method i think 我认为您的“ editText”在onDraw方法中将为null

editText = new EditText(context);

Please let us know when u resolved it 解决问题时请告知我们

I think you should use: 我认为您应该使用:

EditText text = (EditText) findViewById(R.id.editTextId);

You can find this in the top-right of the design xml file. 您可以在设计xml文件的右上角找到它。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM