Android:解决TextView设置textIsSelectable=true后,第一次点击无效

问题:TextView设置textIsSelectable=true后,第一次点击无效,第二次以后有效。

解决:重写TextView,拦截点击事件 并处理。

/**
 * 解决Selectable开启时  点击第一次失效
 * @author sange
 */
class SelectableTextView : AppCompatTextView {

    // 记录按下时间
    private var mLastActionDownTime = 0L

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        var result = false
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> mLastActionDownTime = System.currentTimeMillis()
            MotionEvent.ACTION_MOVE -> {}
            MotionEvent.ACTION_UP -> {
                val actionUpTime = System.currentTimeMillis()
                result = if (actionUpTime - mLastActionDownTime < ViewConfiguration.getLongPressTimeout()) {
                    onVisibilityChanged(this, GONE)
                    callOnClick()
                    true
                }else {
                    //长按事件,即不处理点击事件
                    false
                }
            }
        }
        return if (!result) {
            // 如果没有处理  就走父类方法 使其支持复制粘贴功能
            super.onTouchEvent(event)
        } else {
            true
        }
    }
}
这段代码定义了一个垂直排列的LinearLayout,其中包含一个RelativeLayout作为顶部栏,里面包括一个居中显示的TextView和一个右侧对齐的ImageView。 具体解释如下: - LinearLayout:定义了一个垂直排列的布局。 - android:orientation="vertical":设置LinearLayout的方向为垂直。 - android:layout_width="match_parent":设置LinearLayout的宽度为match_parent,即与父容器宽度相同。 - android:layout_height="match_parent":设置LinearLayout的高度为match_parent,即与父容器高度相同。 - RelativeLayout:定义了一个相对布局。 - android:layout_width="wrap_content":设置RelativeLayout的宽度为包裹内容。 - android:layout_height="48dp":设置RelativeLayout的高度为48dp。 - android:background="@android:color/holo_red_light":设置RelativeLayout的背景颜色为淡红色。 - TextView:定义了一个文本视图。 - android:layout_width="wrap_content":设置TextView的宽度为包裹内容。 - android:layout_height="wrap_content":设置TextView的高度为包裹内容。 - android:text="商品详情":设置TextView的文本内容为“商品详情”。 - android:textSize="24sp":设置TextView的文本大小为24sp。 - android:layout_centerVertical="true":设置TextView在垂直方向上居中对齐。 - android:layout_centerHorizontal="true":设置TextView在水平方向上居中对齐。 - ImageView:定义了一个图像视图。 - android:id="@+id/iv_me":为ImageView设置一个唯一的标识符,以便在代码中引用。 - android:layout_width="wrap_content":设置ImageView的宽度为包裹内容。 - android:layout_height="wrap_content":设置ImageView的高度为包裹内容。 - android:src="@mipmap/icon_shopping_car":设置ImageView的图像源为指定的图片资源。 - android:layout_alignParentRight="true":设置ImageView在父容器的右侧对齐。 - android:layout_centerVertical="true":设置ImageView在垂直方向上居中对齐。 - android:padding="12dp":设置ImageView的内边距为12dp。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两块三刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值