繁体   English   中英

无法在Canvas上绘制EditText

[英]unable draw EditText on Canvas

我正在尝试在位图上写文字,我需要一个edittex来检查键盘的输入。 我在Canvas上找到了用于绘制EditText的代码,但它返回null:

Attempt to invoke virtual method 'void android.widget.EditText.setDrawingCacheEnabled(boolean)' on a null object reference

编辑:全班


public class TextMenu {
 private final Toolbox toolbox;
 private final AppPanel appPanel;
 private final Context context;
 private final MainActivity mainActivity;
 private final Resources resources;
 private Bitmap textTool, Click, textPlace;
 private final float xscaleFactor, yscaleFactor;
 private boolean allowDraw;
 private EditText editText;

public TextMenu(Context context, Resources resources, float xscaleFactor, float yscaleFactor, Toolbox toolbox, AppPanel appPanel, MainActivity mainActivity) {
    this.xscaleFactor = xscaleFactor;
    this.yscaleFactor = yscaleFactor;
    this.resources = resources;
    this.toolbox = toolbox;
    this.appPanel = appPanel;
    this.context = context;
    this.mainActivity = mainActivity;

    textPlace = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.text_place), (int) (xscaleFactor * 93), (int) (yscaleFactor * 193), true);
    textTool = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.text_menu), (int) (xscaleFactor * 480), (int) (yscaleFactor * 100), true);
    Click = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, R.drawable.tools_click), (int) (xscaleFactor * 80), (int) (yscaleFactor * 80), true);

    editText = new EditText(context);
    editText.setText("My Text");
    editText.setWidth(180);
    editText.setBackgroundColor(Color.WHITE);

}

public void draw(Canvas canvas) {
    if (allowDraw) {
        canvas.drawBitmap(textTool, 0, screenH - textTool.getHeight(), null);
        command(canvas);
        editText.setDrawingCacheEnabled(true);
//            editText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            editText.buildDrawingCache(true);
            Bitmap b = editText.getDrawingCache();
            canvas.drawBitmap(b, 100, 100, null);
            // editText.draw(canvas);
            mainActivity.getKeyboard();
        }
    }
}

我怎样才能做到这一点?

我认为您在系统调用draw方法后创建了editText对象。 确保在自定义视图的构造函数中实例化editText。

暂无
暂无

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

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