Android TV Box Development Focus Control

Source: Internet
Author: User

The biggest difference between Android TV app development and general Android development is focus control, where users use the Android TV device primarily to operate the app via the remote control.

CSDN Blog platform to see Daniel summed up the 2 sets of practical focus control, in this collection to share:

The first method:

Use Android to bring the direct control focus up and down around the method. The premise of this approach is that you must know the ID of each view, so you have to go through View.setid (...) when you are laying out. Specify a specific ID for the view, and then pass View.setnextleftview (...). And so on four methods to control the view of the next left and right move after the view reached.

The second method:

In some of the more complex layout, especially the focus of the change in the view of the process to control the view background and font color changes and so on.

For example: There are multiple layout (assuming LAYOUT1, Layout2, LAYOUT3, each layout has several ImageButton),

When you move Layout2 in ImageButton 2.1 from one of the ImageButton 1.1 in LAYOUT1, ImageButton 1.1 is identified as selected, but loses focus, at which point ImageButton 2.1 is selected and gets focus , which is also the case in the process of moving from ImageButton 2.1 to ImageButton 3.1.

For such a situation, you must set the Focus capture actual (Setonfocuschangelistener) for each ImageButton, which is handled in the listener event,

(Note: The following code improvised, presumably that's the meaning – ^_^ –)

ImageButton.setOnFocusChangeListener(){           publicvoidonFocus(boolean Focus){                    if( Focus ){                    // ImageButton 2.1 获焦时, ImageButton 2.1 改变获取焦点背景, ImageButton 1.1也改变失去焦点背景                      else{                     //  ImageButton 2.1 获焦时,  ImageButton 2.1 改变失去焦点背景 ,  ImageButton 3.1也改变获取焦点背景           }              }  }

Also, when you move the focus to ImageButton 3.1, you sometimes need to know which ImageButton is selected on LAYOUT1 and Layout2, so you must also set three ImageButton Variable (identifies the ImageButton object in which layout is selected), and three int variables (identifies the first of the selected layouts).

With these logos, you can easily understand the focus and which one loses focus.

For the control of the upper and lower left and right, it is necessary to capture and process the onkeydown event. Since you already know which layout is selected in the ImageButton, and when you do the upper and lower left and right operation is on your selected view to operate, so in onkeydown you only need to determine which view is selected, The view is then moved according to the key event (moved by the Int ID that was previously set)

(Note: Because it is improvised, there may be some errors in the code, this is just a description of the meaning, – ^_^ –)

Assuming that the selected ImageButton in LAYOUT1 is MFIRSTIMGBTN, the sequence number is mfirstindx;

The selected ImageButton in Layout3 is mthirdimgbtn, and the sequence number is mthirdindx;

The ImageButton inside each layout is in an array,
Assumptions are: ImageButton mimgbtnarray1[], mimgbtnarray2[], mimgbtnarray3[]

The currently selected view is MSENONDIMGBTN Public void OnKeyDown(intKeyCode, KeyEventEvent){if(Event. keycode_drop_up== keycode) {//If the key is pressedMimgbtnarray1[thirdindx].requestfocus; }if(Event. Keycode_drop_down = = keycode) {//If the down key is pressedmimgbtnarray3[Thirdindx].requestfocus; }if(Event. Keycode_drop_left = = keycode) {//If the left button is pressedmimgbtnarray1[thirdindx-1].requestfocus; }if(Event. Keycode_drop_right = = keycode) {//If the right button is pressedmimgbtnarray1[thirdindx+1].requestfocus; }       }

The specific focus event processing is handled in each view's Onfocuschangelistener event.

Android TV Box Development Focus Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.