Kotlin:Android EditText Left Drawable 的单击事件

Kotlin : Click event for Android EditText Left Drawable

下面的代码是点击右边的代码,我应该怎么改成点击左侧可绘制的编辑文本?

fun EditText.onRightDrawableClicked(onClicked: (view: EditText) -> Unit) {
        this.setOnTouchListener { v, event ->
            var hasConsumed = false
            if (v is EditText) {
                if (event.x >= v.width - v.totalPaddingRight) {
                    if (event.action == MotionEvent.ACTION_UP) {
                        onClicked(this)
                    }
                    hasConsumed = true
                }
            }

            hasConsumed
        }
    }

您应该将 event.x >= v.width - v.totalPaddingRight 更改为 event.x <= v.totalPaddingLeft 。如果仍然缺少,您可以随时增加可绘制对象的大小

你应该像这样使用可绘制边界

setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                if ((event.rawX + paddingRight )>= (right - (compoundDrawables.getOrNull(DRAWABLE_RIGHT)?.bounds?.width()
                        ?: 0))
                ) {
                   // right drawable clicked
                } else if (event.rawX - paddingLeft <= ((compoundDrawables.getOrNull(DRAWABLE_LEFT)?.bounds?.width()
                        ?: 0) + left)
                ) {
                    // left drawable clicked
                }
            }
            false
        }