Reprint: http://www.eoeandroid.com/thread-264177-1-1.html
Recently in an Android-based Internet TV client, the development is similar to Phone/pad, but there is a notable place to focus on the problem of control.
On TV to do application development, because the TV interface is relatively large, the above put View/layout is also more, the user operation is mainly through the remote control operation (usually with the remote control,
If the image touch technology is supported, then the view also implements a touchscreen operation similar to the Phone/pad, so the control of the upper and lower left and right movement of the iew will be handled.
After this period of time development, summed up two sets of more useful control focus mechanism. Share here.
The first method: the use of Android comes with direct control of the 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 approach is to control the background of the view and the color change of the font in some complex layout, especially when it comes to changing the focus of the view. For example: There are multiple layout (assuming LAYOUT1, Layout2, LAYOUT3, each layout has a number of ImageButton), when you Layout1 from ImageButton 1.1 Move Layout2 in ImageButton 2.1, at this point ImageButton 1.1 to be identified as selected, but loses focus, at this time ImageButton 2.1 is selected and gets focus, and then from ImageButton 2.1 This is also the case in the process of moving to ImageButton 3.1. For such a situation, you must set the Focus capture actual (Setonfocuschangelistener) on each ImageButton, which is handled in the Listener event (note: The following code is improvised, presumably that means--^_^--)
Imagebutton.setonfocuschangelistener () {
public void onfocus (Boolean Focus) {
if (Focus) {
ImageButton 2.1 Gets burnt, ImageButton 2.1 changes to get focus background, ImageButton 1.1 also changes lost focus background
} else{
ImageButton 2.1, ImageButton 2.1 changes loses focus background, ImageButton 3.1 also changes to get focus background
}
}
}
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 ImageButton in the layout is selected, you are doing it on your chosen view at this point, so in onkeydown you just need to decide 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 the meaning of the description,-^_^-)
Assuming that the selected ImageButton in LAYOUT1 is MFIRSTIMGBTN, the sequence number is mfirstindx;
The selected ImageButton in Layout2 is msenondimgbtn, and the sequence number is msenondindx;
The selected ImageButton in Layout3 is mthirdimgbtn, and the sequence number is mthirdindx;
Each layout inside the ImageButton is in an array, assuming that: ImageButton mimgbtnarray1[], mimgbtnarray2[], mimgbtnarray3[],
The currently selected view is MSENONDIMGBTN
public void OnKeyDown (int keycode, keyevent event) {
if (event. keycode_drop_up== keycode) {//If the key is pressed
Mimgbtnarray1[thirdindx].requestfocus;
}
if (event. Keycode_drop_down = = keycode) {//If the down key is pressed
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 the right button is pressed
mimgbtnarray1[thirdindx+1].requestfocus;
}
}
The specific focus event processing is handled in each view's Onfocuschangelistener event.
Android TV Development Focus control