Fragment监听返回键(Fragment响应Activity的onKeyDown事件)Kotlin版

在Fragment不仅仅只可以监听返回键哦,所有你见过的按键都可以监听,什么A~Z,0~9,F1~F12等等我就不举例子了。至于为什么写这篇博客,因为我的项目是一个Activity+多个Fragment架构的,下面进入正题。。。。直接上代码(原创转载请标明)

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isInterception) {
                if (backListener != null) {
                    backListener!!.onbackForward()
                    return false
                }
            }
        } else if (keyCode == KeyEvent.KEYCODE_STAR) {
            if (startListener != null) {
                startListener!!.onStartButtonPressed()
                return false
            }
        } else if (keyCode == KeyEvent.KEYCODE_POUND) {
            if (poundListener != null) {
                poundListener!!.onPoundButtonPressed()
                return false
            }
        } else if (keyCode == KeyEvent.KEYCODE_PERIOD) {
            if (pointListener != null) {
                pointListener!!.onPointButtonPressed()
                return false
            }
        } else if (keyCode == KeyEvent.KEYCODE_F1) {
            if (f1Listener != null) {
                f1Listener!!.onF1Pressed()
            }
        } else if (keyCode == KeyEvent.KEYCODE_F2) {
            if (f2Listener != null) {
                f2Listener!!.onF2Pressed()
            }
        } else if (keyCode == KeyEvent.KEYCODE_F3) {
            if (f3Listener != null) {
                f3Listener!!.onF3Pressed()
            }
        }
        return super.onKeyDown(keyCode, event)
    }


以上代码是写在MainActivity里面的,在onkeydown事件下监听不同按键的KeyKode(可以参考keycode列表传送门--->http://www.cnblogs.com/bluestorm/p/4886662.html),我这里主要监听了星号键,井号键,英文.,F1,F2,F3,有人会问为什么要监听F1,F2,F3,因为最近在做PDA,按键比较多。废话不多说,那我们怎么把这个Activity中的事件传到Fragment中呢,就是通过接口,上代码:


interface FragmentF1Listener {
    fun onF1Pressed()
}
这里简单的举了一个例子,我们通过接口回调的方式将事件传到Fragment,所以定义了一个方法,onF1Pressed(),接下来在BaseFragment实现这个接口,从而实现接口的方法onF1Pressed(),并且在fragment中初始化Activity中定义的接口对象,下面是代码:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (activity is MainActivity) {
            (activity as MainActivity).backListener = this
            (activity as MainActivity).startListener = this
            (activity as MainActivity).poundListener = this
            (activity as MainActivity).pointListener = this

            (activity as MainActivity).f1Listener = this
            (activity as MainActivity).f2Listener = this
            (activity as MainActivity).f3Listener = this
//            activity.isInterception = true
        }
    }

记得要在销毁时,消除引用,下面是代码

    override fun onDestroy() {
        super.onDestroy()
        if (activity is MainActivity) {
            (activity as MainActivity).backListener = null
            (activity as MainActivity).startListener = null
            (activity as MainActivity).poundListener = null
            (activity as MainActivity).pointListener = null

            (activity as MainActivity).f1Listener = null
            (activity as MainActivity).f2Listener = null
            (activity as MainActivity).f3Listener = null
//            activity.isInterception = true
        }
        if (presenter != null) {
            presenter!!.onDestroy()
        }
    }

这样就可以实现正在Framgnet中监听Back,以及更多的键了


你可能感兴趣的:(Android,Kotlin,Fragment,返回键,返回事件)