我想在画布上点击编辑文字。 我想从用户那里获取输入并将编辑文本设置为画布。
这是我的代码:
@Override
protected void onDraw(Canvas canvas) {
for (Point point : points) {
LinearLayout lL = new LinearLayout(getContext());
EditText editTextView = new EditText(getContext());
editTextView.setFocusableInTouchMode(true);
editTextView.setFocusable(false);
editTextView.setClickable(true);
editTextView.setText("sadsdadadadadsadasds");
editTextView.setVisibility(View.VISIBLE);
lL.addView(editTextView);
lL.setDrawingCacheEnabled(true);
lL.measure(canvas.getWidth(), canvas.getHeight());
lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(lL.getDrawingCache(), point.x, point.y, mPaint);
// canvas.drawText("(0,0)", point.x, point.y, mPaint);
// Log.d(TAG, "Painting: "+point);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
InputMethodManager inputMethodManager;
TextView tv1=new TextView(getContext());
LinearLayout lL = new LinearLayout(getContext());
// return super.onTouchEvent(event);
Point point = new Point();
point.x = (int) event.getX();
point.y = (int) event.getY();
points.add(point);
invalidate();
return true;
}
错误:
问题是EditText是放在画布上而不是点击
答案 0 :(得分:0)
首先不要在画布上绘制,因为触摸仅应用于该画布,在Activity / xml中绘制编辑文本和自定义视图(画布),如果仍然无法点击则尝试将编辑文本置于前面
yourLayout.bringChildToFront(yourEditText);
你的xml是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your custom view here -->
<!-- edittext here -->
<EditText
android:id="@+id/settings_text_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
答案 1 :(得分:0)
这段代码对我有用。请让我知道它没有:
final RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(mainLayout);
mainLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) {
return false;
}
EditText editText = new EditText(YourActivity.this);
// Add whatever you want as size
int editTextHeight = 70;
int editTextWidth = 200;
LayoutParams params = new LayoutParams(editTextWidth, editTextHeight);
//You could adjust the position
params.topMargin = (int) (event.getRawY());
params.leftMargin = (int) (event.getRawX());
mainLayout.addView(editText, params);
editText.requestFocus();
return false;
}
});