Use EditText on Canvas

2019-09-01 03:21发布

问题:

I'm developing an app where I need to draw a EditText on my canvas. I was able to do this, but
I can just see it, not use it. Here is how I've initialized the EditText

et = new EditText(MyActivity.this);
et.setText("edittext");
et.setBackgroundColor(Color.GRAY);
et.requestFocus();
//add it to a LinearLayout
layout.addview(et);

what am I missing to make the EditText actually usable?

回答1:

set EditText InputType like this :

ed.setInputType(2);

or Check Full code based on this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);


    textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout1);

        final EditText ed = new EditText(this);

        ed.setInputType(2);

        ed.setLayoutParams(lparams);

        textFieldsLayout.addView(ed);       


回答2:

This is not a perfect solution.But i hope it gives you some idea :)

/** Called when the activity is first created. */

static Bitmap bmp;
static EditText et;
static ImageView iv;
static Canvas ivCanvas; // We'll be using our own Canvas.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText et = (EditText) findViewById(R.id.editText1);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);

    // Move this up to onCreate
    Bitmap ab = BitmapFactory.decodeResource(getResources(),(R.drawable.ger)) ;
    bmp = convertToMutable(ab); // Initialize it here with the contents of ab. This effectively clones it and makes it mutable.
    ab = null; // Dispose of ab.

    ivCanvas = new Canvas(bmp); // Create our Canvas!

    // Add a TextWatcher
    et.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateCanvas(); // Call the canvas update
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        public void afterTextChanged(Editable s) {
        }
    });
}
public void updateCanvas() {
    ivCanvas.drawColor (Color.BLACK) ;

    ivCanvas.drawBitmap ( bmp , 0 , 0 , null ) ;

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    ivCanvas.drawText(et.getText().toString(),10,10,paint);

    // Everything has been drawn to bmp, so we can set that here, now.
    iv.setImageBitmap(bmp);

    // Removed the "catch" blocks so you can actually know when you're getting errors! Feel free to re-add later.
}


回答3:

You might use this code, in main add this method to get input keys:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    cv = (CustomView) findViewById(R.id.custom_view);
    cv.dispatchKeyEvent(event);
    return super.dispatchKeyEvent(event);
}

in class which extends view class:

@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    Paint p = new Paint();
    p.setTextSize(36);

    LinearLayout layout = new LinearLayout(this.getContext());

    EditText textView = new EditText(this.getContext());
    textView.setVisibility(View.VISIBLE);
    textView.setText(key);
    textView.setX(x);//get x from onTouch method
    textView.setY(y); // get y from onTouch method

    layout.addView(textView);

    layout.measure(canvas.getWidth(), canvas.getHeight());
    layout.layout(50,50, canvas.getWidth(), canvas.getHeight());
    layout.draw(canvas);
} 


 @Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyaction = event.getAction();

    if(keyaction == event.ACTION_DOWN)
    {
        int keyunicode = event.getUnicodeChar(event.getMetaState() );
        char character = (char) keyunicode;
        key += character;
        System.out.println("DEBUG MESSAGE KEY=" + character);
    }
    // you might add if (delete) 
    // https://stackoverflow.com/questions/7438612/how-to-remove-the-last-character-from-a-string
    // method to delete last character 
    invalidate();
    return super.dispatchKeyEvent(event);
}

You will need to store the text in an object, this object contains x, y, width, height, string

in the onTouch method, check if the click is in the same text, then make new EditText >> false and make the old key to be modified.