Click listener for drawableleft in EditText in Android?

后端 未结 2 1519
一个人的身影
一个人的身影 2021-01-03 06:11

Is it possible to add a OnClickListener to on a drawable that is declared in designer as drawableleft

android:drawableLeft=\"@drawa         


        
相关标签:
2条回答
  • 2021-01-03 06:45

    You can achieve it by a 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>
    

    And add onClickListener to the ImageView in your code.

    0 讨论(0)
  • 2021-01-03 06:51

    For drawableleft click listeners:

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

    In case if you want click listener for drawable right use following condition

    if(event.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) 
    
    0 讨论(0)
提交回复
热议问题