程序员Android开发Android开发

登录界面一定会用到的-CompoundDrawableClick

2019-11-21  本文已影响0人  追风筝的boy

需求

分析

<com.lliujun.commonutil.TestEditText
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:drawableStart="@drawable/ic_rectangle"
        android:drawableEnd="@drawable/ic_rectangle"
        android:drawableTop="@drawable/ic_rectangle"
        android:drawableBottom="@drawable/ic_rectangle"
        android:drawablePadding="10dp"
        />

Talk is cheep, Show you my code

fun TextView.contentRect(): Rect {
    return Rect(
            compoundPaddingLeft,
            compoundPaddingTop,
            width - compoundPaddingRight,
            height - compoundPaddingBottom
    )
}
fun TextView.isTouchLeftCompoundDrawable(event: MotionEvent): Boolean {
    val drawable = compoundDrawables[0] ?: return false
    val contentRect = contentRect()
    val rect = RectF(
            (contentRect.left - drawable.intrinsicWidth - compoundDrawablePadding).toFloat(),
            (contentRect.top).toFloat(),
            (contentRect.left - compoundDrawablePadding).toFloat(),
            (contentRect.bottom).toFloat()
    )
    return rect.contains(event.x, event.y)
}
class CompoundDrawableDetector(
        private val textView: TextView,
        private val onClickListener: OnClickListener
) {

    private var detectedCompoundDrawableClickEvent = false
    private var handler = Handler()
    /**
     * @return 返回true,表示该事件需要在内部进行处理,false 则表示内部没有进行处理,可以交给外部来处理
     * */
    fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_DOWN) {
            detectCompoundDrawableClickEvent(event)
        } else if (event.action == MotionEvent.ACTION_UP) {
            handler.postDelayed({ detectedCompoundDrawableClickEvent = false }, 50)
        }
        return detectedCompoundDrawableClickEvent
    }


    private fun detectCompoundDrawableClickEvent(event: MotionEvent) {
        with(textView) {
            for ((index, drawable) in compoundDrawables.withIndex()) {
                if (drawable == null) continue
                when (index) {
                    DRAWABLE_LEFT ->
                        if (isTouchLeftCompoundDrawable(event)) {
                            detectedCompoundDrawableClickEvent = true
                            onClickListener.onLeftClick()
                        }
                    DRAWABLE_TOP ->
                        if (isTouchTopCompoundDrawable(event)) {
                            detectedCompoundDrawableClickEvent = true
                            onClickListener.onTopClick()
                        }
                    DRAWABLE_RIGHT ->
                        if (isTouchRightCompoundDrawable(event)) {
                            detectedCompoundDrawableClickEvent = true
                            onClickListener.onRightClick()
                        }
                    DRAWABLE_BOTTOM ->
                        if (isTouchBottomCompoundDrawable(event)) {
                            detectedCompoundDrawableClickEvent = true
                            onClickListener.onBottomClick()
                        }
                }
            }
        }
    }

    interface OnClickListener {
        fun onLeftClick() {}
        fun onRightClick() {}
        fun onTopClick() {}
        fun onBottomClick() {}
    }

    companion object {
        private const val DRAWABLE_LEFT = 0
        private const val DRAWABLE_TOP = 1
        private const val DRAWABLE_RIGHT = 2
        private const val DRAWABLE_BOTTOM = 3
    }
}

如何使用? So easy

class TestEditText(context: Context, attrs: AttributeSet) : EditText(context, attrs) {

    private val compoundDrawableDetector by lazy {
        CompoundDrawableDetector(this, object : CompoundDrawableDetector.OnClickListener {
            override fun onLeftClick() {
                toast("left click")
            }
            override fun onRightClick() {
                toast("right click")
            }
            override fun onTopClick() {
                toast("click top")
            }
            override fun onBottomClick() {
                toast("click bottom")
            }
        })
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return if (compoundDrawableDetector.onTouchEvent(event)) {
            true
        } else {
            super.onTouchEvent(event)
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读