android tv基础之焦点(二)

来源:互联网 发布:linux axel 编辑:程序博客网 时间:2024/05/05 12:47

这个是关于自定义控件的焦点切换,当切换过来的时候保持之前选中的view的状态,界面如下:


左边的是一个自定义的LinearLayout,右边是一个在xml中一个普通的LinearLayout,里面放了5个button按钮,代码如下:

<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"     ><com.example.tvdemo1.CustomLinearLayout    android:id="@+id/custom_ll"    android:layout_width="600dp"    android:layout_height="match_parent"    ></com.example.tvdemo1.CustomLinearLayout><LinearLayout        android:layout_width="600dp"        android:layout_height="match_parent"        android:orientation="vertical"        android:layout_marginLeft="40dp"        >        <Button             android:id="@+id/button6"            android:layout_width="400dp"            android:layout_height="wrap_content"            android:text="button6"            />        <Button             android:id="@+id/button7"            android:layout_width="400dp"            android:layout_height="wrap_content"            android:text="button7"            />        <Button             android:id="@+id/button8"            android:layout_width="400dp"            android:layout_height="wrap_content"            android:text="button8"            />        <Button             android:id="@+id/button9"            android:layout_width="400dp"            android:layout_height="wrap_content"            android:text="button9"            />        <Button             android:id="@+id/button10"            android:layout_width="400dp"            android:layout_height="wrap_content"            android:text="button10"            /></LinearLayout></LinearLayout></span>


CustomLinearLayout.java

<span style="font-size:18px;">package com.example.tvdemo1;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.graphics.Color;import android.util.AttributeSet;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class CustomLinearLayout extends LinearLayout {private View firstView;protected static final String TAG = "CustomLinearLayout";private boolean isFirst;private View preView;private int index;private OnSelectViewListener mOnSelectViewListener;private Button mSelectView;private List<View> views = new ArrayList<>();public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public CustomLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);}public CustomLinearLayout(Context context) {super(context);}@Overridepublic void addView(View child) {super.addView(child);views.add(child);if(!isFirst){isFirst = true;firstView = child;firstView.setBackgroundColor(Color.RED);}} @Overridepublic boolean dispatchKeyEvent(KeyEvent event) { if(event.getAction()==KeyEvent.ACTION_DOWN){ switch (event.getKeyCode()) {case KeyEvent.KEYCODE_DPAD_UP:if(index>0){setViewNormalBg(views.get(index));setViewFouceBg(views.get(index-1));index--;}break;case KeyEvent.KEYCODE_DPAD_DOWN:if(index<views.size()-1){index++;}setViewNormalBg(views.get(index-1));setViewFouceBg(views.get(index));break;case KeyEvent.KEYCODE_DPAD_RIGHT:views.get(index).setFocusable(false);views.get(index).setFocusableInTouchMode(false);setViewNormalBg(views.get(index));mOnSelectViewListener.onSelectView(mSelectView);break;} }return super.dispatchKeyEvent(event);} public void setViewFouceBg(View view){view.setBackgroundColor(Color.RED);}public void setViewNormalBg(View view){view.setBackgroundColor(Color.TRANSPARENT);}public void setSelectIndex(int index){setViewFouceBg(views.get(index));}public int getSelectIndex() {return index;}interface OnSelectViewListener{void onSelectView(Button view);}public void setOnSelectViewListener(OnSelectViewListener onSelectViewListener) {this.mOnSelectViewListener = onSelectViewListener;}public void setSlectView(Button view){this.mSelectView = view;}}</span>


MainActivity.java

<span style="font-size:18px;">package com.example.tvdemo1;import com.example.tvdemo1.CustomLinearLayout.OnSelectViewListener;import android.annotation.SuppressLint;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.KeyEvent;import android.view.View;import android.view.View.OnFocusChangeListener;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.LinearLayout;@SuppressLint("NewApi")public class MainActivity extends ActionBarActivity implements  OnFocusChangeListener {private static final String TAG = "MainActivity";private Button button6,button7,button8,button9,button10;private CustomLinearLayout custom_ll;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();setListener();}private void setListener() {custom_ll.setOnSelectViewListener(new OnSelectViewListener() {@Overridepublic void onSelectView(Button view) {if(view==null){button6.requestFocus();}else{view.setFocusable(true);view.setFocusableInTouchMode(true);view.requestFocus();}}});button6.setOnFocusChangeListener(this);button7.setOnFocusChangeListener(this);button8.setOnFocusChangeListener(this);button9.setOnFocusChangeListener(this);button10.setOnFocusChangeListener(this);button6.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(event.getAction()==KeyEvent.ACTION_DOWN){switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:return true;case KeyEvent.KEYCODE_DPAD_DOWN:button7.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_LEFT:setView(button6);custom_ll.setSelectIndex(custom_ll.getSelectIndex());custom_ll.setSlectView(button6);return true;}}return false;}});button7.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(event.getAction()==KeyEvent.ACTION_DOWN){switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:button6.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_DOWN:button8.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_LEFT:setView(button7);custom_ll.setSelectIndex(custom_ll.getSelectIndex());custom_ll.setSlectView(button7);return true;}}return false;}});button8.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(event.getAction()==KeyEvent.ACTION_DOWN){switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:button7.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_DOWN:button9.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_LEFT:setView(button8);custom_ll.setSelectIndex(custom_ll.getSelectIndex());custom_ll.setSlectView(button8);return true;}}return false;}});button9.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(event.getAction()==KeyEvent.ACTION_DOWN){switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:button8.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_DOWN:button10.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_LEFT:setView(button9);custom_ll.setSelectIndex(custom_ll.getSelectIndex());custom_ll.setSlectView(button9);return true;}}return false;}});button10.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(event.getAction()==KeyEvent.ACTION_DOWN){switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:button9.requestFocus();return true;case KeyEvent.KEYCODE_DPAD_DOWN:return true;case KeyEvent.KEYCODE_DPAD_LEFT:setView(button10);custom_ll.setSelectIndex(custom_ll.getSelectIndex());custom_ll.setSlectView(button10);return true;}}return false;}});}private void initViews() {custom_ll = (CustomLinearLayout) findViewById(R.id.custom_ll);custom_ll.setOrientation(LinearLayout.VERTICAL);addViews();button6 = (Button) findViewById(R.id.button6);button7 = (Button) findViewById(R.id.button7);button8 = (Button) findViewById(R.id.button8);button9 = (Button) findViewById(R.id.button9);button10= (Button) findViewById(R.id.button10);}private void addViews() {for(int i=0;i<10;i++){Button button = new Button(this);button.setText("自定义button--"+i);custom_ll.addView(button);}}@Overridepublic void onFocusChange(View v, boolean hasFocus) {if(hasFocus){switch (v.getId()) {case R.id.button6:setViewFouceBg(button6);break;case R.id.button7:setViewFouceBg(button7);break;case R.id.button8:setViewFouceBg(button8);break;case R.id.button9:setViewFouceBg(button9);break;case R.id.button10:setViewFouceBg(button10);break;}}else{setViewNormalBg();}}public void setViewFouceBg(View view){view.setBackgroundColor(Color.RED);}public void setViewNormalBg(){button6.setBackgroundColor(Color.TRANSPARENT);button7.setBackgroundColor(Color.TRANSPARENT);button8.setBackgroundColor(Color.TRANSPARENT);button9.setBackgroundColor(Color.TRANSPARENT);button10.setBackgroundColor(Color.TRANSPARENT);}public void setView(Button button){button.setFocusable(false);button.setFocusableInTouchMode(false);//button.setBackgroundColor(Color.TRANSPARENT);}public void setViewsNoFouce(){button6.setFocusable(false);button6.setFocusableInTouchMode(false);button7.setFocusableInTouchMode(false);button8.setFocusableInTouchMode(false);button9.setFocusableInTouchMode(false);button10.setFocusableInTouchMode(false);button7.setFocusable(false);button8.setFocusable(false);button9.setFocusable(false);button10.setFocusable(false);}}</span>
整个代码都很简单,我也是做tv开发不久,所以会从最简单的写起!


0 0