我有左右两个按钮,我只想同时按下两个按钮,然后两个日志都将显示。
val handleTouch = View.OnTouchListener { v, event ->
val leftDown : Int
when(v){
btn_leftThumb -> when (event.action) {
MotionEvent.ACTION_DOWN -> pressed(true, null)
MotionEvent.ACTION_UP -> pressed(false, null)
}
btn_rightThumb -> when (event.action) {
MotionEvent.ACTION_DOWN -> pressed(null, true)
MotionEvent.ACTION_UP -> pressed(null, false)
}
}
true
}
btn_leftThumb.setOnTouchListener(handleTouch)
btn_rightThumb.setOnTouchListener(handleTouch)
private fun pressed(left:Boolean?, right:Boolean?){
if(left == true && right == true){
Log.i(TAG, "BOTH")
}else if(left == false && right == true){
Log.i(TAG, "RIGHT")
}else if(left == true && right == false){
Log.i(TAG, "LEFT")
}
}
答案 0 :(得分:3)
您将必须将每个按钮的按下状态存储在某个全局变量和onTouchListener更新中,然后使用方法pressed()
检查两个变量
答案 1 :(得分:0)
这是解决方案:
private var actionLeft:String =“”
private var actionRight:字符串=“”
和onCreate:
val handleTouch = View.OnTouchListener {v,event->
when(v){
btn_leftThumb -> when (event.action) {
MotionEvent.ACTION_DOWN -> {actionLeft = "DOWN"
pressed(actionLeft, actionRight)
}
MotionEvent.ACTION_UP -> {actionLeft = "UP"
pressed(actionLeft, actionRight)
}
}
btn_rightThumb -> when (event.action) {
MotionEvent.ACTION_DOWN -> {actionRight = "DOWN"
pressed(actionLeft, actionRight)
}
MotionEvent.ACTION_UP -> {actionRight = "UP"
pressed(actionLeft, actionRight)
}
}
}
true
}
btn_leftThumb.setOnTouchListener(handleTouch)
btn_rightThumb.setOnTouchListener(handleTouch)
按下私人乐趣(左:字符串?,右:字符串?){
if(right == "DOWN" && left =="DOWN") {
Log.d(TAG, "DOWN")
}else if (right == "UP" && left =="DOWN"){
Log.d(TAG, "CANCELLED")
}else if (right == "DOWN" && left =="UP"){
Log.d(TAG, "CANCELLED")
}else if (right == "UP" && left =="UP"){
Log.d(TAG, "CANCELLED")
}
}