How to make EditText Clickable on canvas?

守給你的承諾、 提交于 2019-12-07 17:29:25

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);

your xml be something like this

<?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>

This code worked for me. Please let me know it doesn't:

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;
   }
 });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!