在Android的EditText中点击drawableleft的监听器?

3 android imageview onclicklistener

是否可以OnClickListener在设计器中声明的drawable上添加一个todrawableleft

android:drawableLeft="@drawable/ic_password"
Run Code Online (Sandbox Code Playgroud)

或者有没有办法ImageView在左侧添加一个EditText

Ran*_*mar 7

对于drawableleft点击监听器:

 editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

       if(event.getRawX() <= (editText.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()))
        {
              // your action here
             return true;
        }
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

如果你想要点击监听器可绘制正确使用以下条件

if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) 
Run Code Online (Sandbox Code Playgroud)


KER*_*iii 0

您可以通过RelativeLayout 来实现它:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <ImageView
        android:id="@+id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_iamge"/>

    <EditText
        android:id="@+id/text_id"
        android:layout_toRightOf="@id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="16dp"
        android:text="Your text" />

 </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

并添加onClickListenerImageView您的代码中。