美文网首页
监听android:drawableLeft和android:d

监听android:drawableLeft和android:d

作者: CentForever | 来源:发表于2021-01-06 14:07 被阅读0次

监听android:drawableLeft和android:drawableRight点击事件

DrawableUtil

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.widget.TextView

class DrawableUtil(private val mTextView: TextView, l: OnDrawableListener?) {
    /**
     * TextView四周drawable的序号。
     * 0 left,  1 top, 2 right, 3 bottom
     */
    companion object {
        const val LEFT = 0
        const val RIGHT = 2
    }
    private var listener: OnDrawableListener? = null

    interface OnDrawableListener {
        fun onLeft(v: View?, left: Drawable?)
        fun onRight(v: View?, right: Drawable?)
    }

    @SuppressLint("ClickableViewAccessibility")
    private val mOnTouchListener = OnTouchListener { v, event ->
        when (event.action) {
            MotionEvent.ACTION_UP -> if (listener != null) {
                val drawableLeft = mTextView.compoundDrawables[LEFT]
                if (drawableLeft != null && event.rawX <= mTextView.left + drawableLeft.bounds.width()) {
                    listener?.onLeft(v, drawableLeft)
                    return@OnTouchListener true
                }
                val drawableRight = mTextView.compoundDrawables[RIGHT]
                if (drawableRight != null && event.rawX >= mTextView.right - drawableRight.bounds.width()) {
                    listener?.onRight(v, drawableRight)
                    return@OnTouchListener true
                }
            }
        }
        false
    }

    init {
        mTextView.setOnTouchListener(mOnTouchListener)
        listener = l
    }
}

实现

DrawableUtil(findViewById<TextView>(R.id.mTextView2), object : OnDrawableListener {
            override fun onLeft(v: View?, left: Drawable?) {
                Toast.makeText(applicationContext, "left", Toast.LENGTH_SHORT).show()
            }

            override fun onRight(v: View?, right: Drawable?) {
                Toast.makeText(applicationContext, "right", Toast.LENGTH_SHORT).show()
            }
        })

xml

android:clickable="true"
import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.view.MotionEvent
import android.view.View
import android.widget.TextView

class TextViewDrawableClickUtil(private val mTextView: TextView, listener: OnDrawableClickListener?) {
    /**
     * TextView四周drawable的序号。
     * 0 left,  1 top, 2 right, 3 bottom
     */
    companion object {
        private const val LEFT = 0
        private const val RIGHT = 2
    }

    interface OnDrawableClickListener {
        fun onLeftDrawableClick(v: View, left: Drawable)
        fun onRightDrawableClick(v: View, right: Drawable)
    }

    @SuppressLint("ClickableViewAccessibility")
    private val mOnTouchListener = View.OnTouchListener { v, event ->
        when (event.action) {
            MotionEvent.ACTION_UP -> if (listener != null) {
                val drawableLeft = mTextView.compoundDrawables[LEFT]
                if (drawableLeft != null && event.rawX <= mTextView.left + drawableLeft.bounds.width()) {
                    listener.onLeftDrawableClick(v, drawableLeft)
                    return@OnTouchListener true
                }
                val drawableRight = mTextView.compoundDrawables[RIGHT]
                if (drawableRight != null && event.rawX >= mTextView.right - drawableRight.bounds.width()) {
                    listener.onRightDrawableClick(v, drawableRight)
                    return@OnTouchListener true
                }
            }
        }
        false
    }

    init {
        mTextView.setOnTouchListener(mOnTouchListener)
    }
}

相关文章

网友评论

      本文标题:监听android:drawableLeft和android:d

      本文链接:https://www.haomeiwen.com/subject/nbagnktx.html