Android TV box development focus control

Source: Internet
Author: User

Android TV box development focus control

The biggest difference between Android TV app development and general Android development is focus control. users use Android TV devices to operate apps through remote control.

 

The CSDN blog platform displays two sets of practical focus control summarized by Daniel:

Method 1:

The Android-based direct control focus is adopted. The premise of this method is that you must know the id of each view. Therefore, you must use view. setId (…) for layout (...) Specify the specific view ID, and then use view. setNextLeftView (...) And so on.

Method 2:

In some complex Layout scenarios, especially when the focus of the View changes, you must control the background and font color of the view.

For example, if there are multiple Layout instances (for example, Layout1, Layout2, and Layout3, each Layout contains several ImageButton instances ),

When you move ImageButton 1.1 in Layout2 from an ImageButton 2.1 in Layout1, at this time, ImageButton 1.1 is marked as selected, but the focus is lost. At this time, ImageButton 2.1 is selected and the focus is obtained, this is also the case when moving from ImageButton 2.1 to ImageButton 3.1.

In this case, you must set the actual focus capture (setOnFocusChangeListener) for each ImageButton and process it in this listener event,

(Note: The code below is probably written in an impromptu manner.-^_^ -)

ImageButton. setOnFocusChangeListener () {public void onFocus (boolean Focus) {if (Focus) {// when ImageButton 2.1 gets Focus, ImageButton 2.1 changes to get Focus background, imageButton 1.1 also changes the background with no focus} When else {// ImageButton 2.1 gets focus, ImageButton 2.1 changes the background with no focus, and ImageButton 3.1 also changes the background with focus }}}
 

When you move the current focus to ImageButton 3.1, you sometimes need to know which ImageButton is selected on Layout1 and Layout2, therefore, you must set three ImageButton variables (the ImageButton object in the selected layout) and three int variables (the number of the selected layout ).

With these identifiers, you can easily understand the focus and which one has lost the focus.

For upper, lower, and left control, capture and process the event in OnKeyDown. Because you already know which ImageButton in the Layout is selected, you can perform the upper, lower, and lower operations on the selected View, therefore, in OnKeyDown, you only need to first determine which View is selected, and then move the View based on the key event (move with the int ID previously set)

(Note: Some code may be incorrect due to impromptu writing. Here we only describe this meaning,-^_^ -)

 

Assume that the selected ImageButton in Layout1 is mFirstImgBtn and the serial number is mFirstIndx;

In Layout3, the selected ImageButton is mThirdImgBtn and the serial number is mThirdIndx;

ImageButton in each Layout is in an array,
Assume: ImageButton mImgBtnArray1 [], mImgBtnArray2 [], mImgBtnArray3 []

 
The currently selected view is mSenondImgBtn public void OnKeyDown (int keyCode, KeyEvent event) {if (event. KEYCODE_DROP_UP = keyCode) {// If you press mImgBtnArray1 [ThirdIndx]. requestFocus;} if (event. KEYCODE_DROP_DOWN = keyCode) {// If you press mImgBtnArray3 [ThirdIndx]. requestFocus;} if (event. KEYCODE_DROP_LEFT = keyCode) {// If the left button is pressed, mImgBtnArray1 [ThirdIndx-1]. requestFocus;} if (event. KEYCODE_DROP_RIGHT = keyCode) {// If you right-click mImgBtnArray1 [ThirdIndx + 1]. requestFocus ;}}

The specific focal point event processing is performed in the OnFocusChangeListener event of each View.

Related Article

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.