如何覆盖 Kotlin 中的performClick
以避免警告?
next.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action){
MotionEvent.ACTION_DOWN -> {
val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
next.setImageDrawable(icon)
}
MotionEvent.ACTION_UP -> {
//view.performClick()
next.setImageResource(R.drawable.layer_bt_next)
}
}
return@OnTouchListener true
})
view.performClick
不起作用。
答案 0 :(得分:8)
尝试这种方式:
next.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> //Do Something
}
return v?.onTouchEvent(event) ?: true
}
})
答案 1 :(得分:4)
我认为您的解决方案实际上不会解决警告提出的问题。警告声明某些辅助功能使用$ cat local_pvc.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-local-pv
annotations:
"volume.alpha.kubernetes.io/node-affinity": '{
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{ "matchExpressions": [
{ "key": "kubernetes.io/hostname",
"operator": "In",
"values": ["my-node"] <--- change the node name to yours
}
]}
]}
}'
spec:
capacity:
storage: 5Gi <----- change the size to your need
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /mnt/disks/vol1 <----- change the path to yours
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: example-local-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi <----- change the size to your need
storageClassName: local-storage
----
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
激活按钮。如果您查看performClick()
课程,View
功能会直接调用performClick()
,这意味着onClickListener
中的代码将不会被执行(onTouchListener
)这些辅助功能,因为视图永远不会被物理触摸,因此您的next.setImageResource(R.drawable.layer_bt_next)
代码将无法运行。您必须执行以下任一操作之一:
onTouch
的视图子类化,并覆盖onTouchListener
以执行代码,或performClick
。您可以在onTouchListener类中实现onClickListener
,然后从onClickListener
(现在有onClick()
)手动调用onTouchListener
,然后将可执行代码移至view.performClick()
覆盖。您还必须在视图中设置onClick
和onTouchListener
。
答案 2 :(得分:3)
好的,我通过覆盖OnTouch监听器解决了我自己的问题。
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (view) {
next -> {
Log.d("next", "yeyy")
when (motionEvent.action){
MotionEvent.ACTION_DOWN -> {
val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
next.setImageDrawable(icon)
}
MotionEvent.ACTION_UP -> {
view.performClick()
next.setImageResource(R.drawable.layer_bt_next)
}
}
}
previous -> {
//ingredients here XD
}
}
return true
}
通过这种方式,我可以调用单个onTouch并将其实现为多个按钮,也可以使用onClick by:
view.performClick()
别忘了实施:
View.OnTouchListener
设置监听器:
next.setOnTouchListener(this)
previous.setOnTouchListener(this)
答案 3 :(得分:1)
我不确定这是你看到的同一个问题,但是因为我发现这个页面正在搜索我的问题,我想我会添加我的经验来帮助其他人:)
在我的情况下,正在生成警告,因为可空视图可能是Void
类型。请致电以下人员:
nullableView?.setOnTouchListener(this)
产生错误:
Custom view Void has setOnTouchListener called on it but does not override performClick
在这种情况下,在为我设置侦听器之前执行空检查并转换为View
,因为View
将覆盖performClick
:
if (nullableView != null) (nullableView as View).setOnTouchListener(this)
答案 4 :(得分:0)
private fun closeKeyboard(binding: ContollerMeterBinding) {
binding.scrollView.apply {
setOnTouchListener(OnTouchListener { v, event ->
if (event != null && event.action == MotionEvent.ACTION_MOVE) {
val imm =
activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val isKeyboardUp = imm.isAcceptingText
if (isKeyboardUp) {
imm.hideSoftInputFromWindow(v.windowToken, 0)
}
}
performClick()
false
})
}
}
这对我有用:(与 onTouch 事件不直接相关,但产生相同的警告,可能对某人有帮助)