如何在EditText DrawableRight上执行Click事件?

时间:2016-02-17 18:29:00

标签: android

我想单击EditText DrawableRight和EditText editable为Enable。 怎么可能? 我的代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edt_profile_mobileno"
        android:layout_width="match_parent"
        android:drawableRight="@drawable/edit"
        android:layout_height="wrap_content"
        android:editable="false"
        android:hint="Mobile No"/>
</LinearLayout>

给我正确的解决方案。Plese show this image

1 个答案:

答案 0 :(得分:1)

只需将setOnTouchListener事件用于编辑视图即可。此示例取自here

editComment.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.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() >= (editComment.getRight() -    editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                // your action here

             return true;
            }
        }
        return false;
    }
});