1

我正在为我的 android 电视盒创建一个简单的应用程序,它使用 webview 对象来显示一些流 url,并使用遥控器(硬件键盘)的 PGup 和 PGdown 选择它们。

我正在重写方法 onKeyUp,但不幸的是我的应用程序似乎没有检测到任何按键。这是一些代码摘录:

package com.dm.tutorialwebview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.Menu
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.util.Log
import android.view.MotionEvent


class MainActivity : AppCompatActivity() {

    var webview: WebView? = null
    data class Channel(val number: Int, val name:String, val url: String )

    object ChannelList {
        private val list = mutableListOf<Channel>()
        private var curChannel: Int = 0
[..]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ChannelList.addItem(Channel(1,"channel1","https://...3"))
        ChannelList.addItem(Channel(2,"channel2","https://..."))
        ChannelList.addItem(Channel(3,"channel3","https://..."))

        webview = findViewById(R.id.myweb)
        webview!!.webViewClient = WebViewClient()
        webview!!.settings.javaScriptEnabled = false
        webview!!.webChromeClient = WebChromeClient()
        webview!!.settings.domStorageEnabled = true
        webview!!.settings.builtInZoomControls = false     
        webview!!.settings.setSupportZoom(false)           
        webview!!.overScrollMode = WebView.OVER_SCROLL_NEVER
        webview!!.settings.useWideViewPort = true
        webview!!.setInitialScale(1)

        webview!!.loadUrl(ChannelList.getChannelUrl())

    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        Log.i("TAG", "onKeyUp is been called");
        return when (keyCode) {
            KeyEvent.KEYCODE_PAGE_UP -> {
                ChannelList.nextChannel()
                webview!!.loadUrl(ChannelList.getChannelUrl())
                true
            }
            KeyEvent.KEYCODE_PAGE_DOWN -> {
                ChannelList.prevChannel()
                webview!!.loadUrl(ChannelList.getChannelUrl())
                true
            }
            KeyEvent.KEYCODE_1 -> {
                ChannelList.setChannel(1)
                webview!!.loadUrl(ChannelList.getChannelUrl())
                true
            }
            else -> super.onKeyUp(keyCode, event)
        }

    }


}

方法 onKeyUp 似乎根本没有被触发。关于此代码可能有什么问题的任何提示?

谢谢并恭祝安康

4

1 回答 1

1

感谢@Ashwini-violet,我用 dispathKeyEvent 替换了 onKeyUp。

我使用一种解决方法将输入限制为每 250 毫秒一次;不是很有礼貌,但它的工作。

    var lastclick : Long = 0
    var keyDelay : Int = 250        
    
    [..]

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
    return when (event!!.keyCode) {
        KeyEvent.KEYCODE_PAGE_UP , KeyEvent.KEYCODE_DPAD_UP -> {
            if ((SystemClock.currentThreadTimeMillis() - lastclick) < keyDelay) true
            else {
                ChannelList.nextChannel()
                webview!!.clearHistory()
                webview!!.clearCache(true)
                showChannelName()
                webview!!.loadUrl(ChannelList.getChannelUrl())
                lastclick = SystemClock.currentThreadTimeMillis()
                true
            }
        }
于 2021-02-13T15:31:51.017 回答