gpt4 book ai didi

android - 如何使 EditText 在 Canvas 上可点击?

转载 作者:行者123 更新时间:2023-12-05 00:07:25 25 4
gpt4 key购买 nike

我想在 Canvas 上点击编辑文本。我想从用户那里获取输入并将该编辑文本设置为 Canvas 。

这是我的代码:

@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 被放置在 Canvas 上但不能点击

最佳答案

首先不要在 Canvas 上绘制它,因为触摸仅应用于该 Canvas ,在您的 Activity/xml 中绘制编辑文本和您的自定义 View ( Canvas ),如果仍然不可点击,请尝试将编辑文本置于最前面

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>

关于android - 如何使 EditText 在 Canvas 上可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21808582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com