如何使EditText在画布上可点击? [英] How to make EditText Clickable on canvas?

查看:69
本文介绍了如何使EditText在画布上可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在画布上的水龙头上放置编辑文本. 我想从用户那里获取输入并将该编辑文本设置为画布.

i want to place edit text on tap on canvas. i want to take input from user and set that edit text to 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上但不能单击

PROBLEM is that EditText is Getting placed on Canvas but not clickble

推荐答案

首先不要将其绘制在画布上,因为触摸仅应用于该画布,因此在Activity/xml中绘制编辑文本和自定义视图(画布),如果仍然无法点击,请尝试将编辑文字放在最前面

First dont draw it on canvas, as touches are applied only to that canvas, in your Activity/xml draw the edit text and your custom view (canvas), if still not clickable try to bring edit text to front

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>

这篇关于如何使EditText在画布上可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆