问题: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
}
}
}