using remote control for android TV application

痞子三分冷 提交于 2020-02-02 16:33:12

问题


I'm developing an Android application for TV when I'm trying to test it in the real Android TV I can't navigate to any button or anything using the remote control buttons up, down, left, right.

I have searched about it and I found that I have to use (Enable D-pad Navigation ) but I don't know how to use this , I didn't find any code or tutorial for it My application is so simple. It makes dynamic layout beside each other and each layout has only one button and one recycle view.


回答1:


By default Android implements basic D-Pad navigation inferred from the layout distribution. It works quite well if you use mostly lists or linear layouts.

The key difference is that the views are on the focused state instead of selected, so if you are using custom background i.e. for buttons it may look like it is not working while in fact it is.

You can try it out on an emulator using the cursor keys on the keyboard.

Alternatively or if the inferred navigation does not work you can define the nextFocusUp, Down, Left and Right per view.

It is all quite well explained on the official documentation: https://developer.android.com/training/tv/start/navigation.html




回答2:


try this,

@Override
    public boolean onKeyDown(int keyCode, KeyEvent events) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.onKeyDown(keyCode, events);
    }

if in case call is not getInside above ,then try this:

  @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.dispatchKeyEvent(event);
    }


来源:https://stackoverflow.com/questions/45963749/using-remote-control-for-android-tv-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!