DialogFragment 监听按键事件的方法(onkeydown)

                我们在TV软件开发的时候,会使用DialogFragment,有时候要对它的按键事件进行监听,但是DialogFragment的监听方法和其它的不一样。

 

           方法:

            在onCreateView中加入

this.getDialog().setOnKeyListener(new OnKeyListener()
        {
           public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event){
           if (keyCode == KeyEvent.KEYCODE_SEARCH)
             return true; // pretend we've processed it
           else
             return false; // pass on to be processed as normal
         }
       });

        即可,通过判断各种事件,进行监听处理。希望能够帮到需要的朋友。

         还有 就是  return false和return true是是否允许事件下传,return true是中断事件,那么下面的就接受不到按键信息了,只有在return false的时候 才会事件继续向下传递。

原文地址:https://www.cnblogs.com/yejiurui/p/3757493.html