我正在开发一个应用,我需要在画布上绘制一个EditText。我能做到这一点,但是 我可以看到它,而不是使用它。这是我如何初始化EditText
et = new EditText(MyActivity.this);
et.setText("edittext");
et.setBackgroundColor(Color.GRAY);
et.requestFocus();
//add it to a LinearLayout
layout.addview(et);
我缺少什么使EditText实际可用?
答案 0 :(得分:0)
设置EditText InputType,如下所示:
ed.setInputType(2);
或根据此检查完整代码:
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);
答案 1 :(得分:0)
这不是一个完美的解决方案。但我希望它能给你一些想法:)
/** 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.
}
答案 2 :(得分:0)
您可以使用此代码,在main
中添加此方法以获取输入键:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
cv = (CustomView) findViewById(R.id.custom_view);
cv.dispatchKeyEvent(event);
return super.dispatchKeyEvent(event);
}
在扩展视图类的类中:
@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);
}
您需要将文本存储在对象中,此对象包含x, y, width, height, string
在onTouch
方法中,检查点击是否在同一文本中,然后制作new EditText >> false
并修改旧密钥。