android回车事件,Android TV入门问题,为何onKeyDown里拦截不到回车事件
非常新手在入门,做的是安卓TV,想判断按了遥控器的确认键。但是发现,其他键都能进入onKeyDown事件,唯独是确认键不会进入onKeyDown事件,如果确实没办法,只能在dispatchKeyEvent拦截了。override fun dispatchKeyEvent(event: KeyEvent): Boolean {Log.d("dispatchKeyEvent", "dispatchKe
非常新手在入门,做的是安卓TV,想判断按了遥控器的确认键。但是发现,其他键都能进入onKeyDown事件,唯独是确认键不会进入onKeyDown事件,如果确实没办法,只能在dispatchKeyEvent拦截了。
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
Log.d("dispatchKeyEvent", "dispatchKeyEvent(), action=" + event.action + " keycode=" + event.keyCode)
return super.dispatchKeyEvent(event)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_DPAD_CENTER ->
Toast.makeText(this, "你按下中间键", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_DPAD_DOWN ->
Toast.makeText(this, "你按下下间键", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_DPAD_LEFT ->
Toast.makeText(this, "你按下左方向键", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_DPAD_RIGHT ->
Toast.makeText(this, "你按下右方向键", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_DPAD_UP ->
Toast.makeText(this, "你按下上方向键", Toast.LENGTH_SHORT).show()
KeyEvent.KEYCODE_BACK ->
Toast.makeText(this, "你按下后退键", Toast.LENGTH_SHORT).show()
}
return super.onKeyDown(keyCode, event)
}
更多推荐
所有评论(0)