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


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

        


CustomLinearLayout.java

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 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


MainActivity.java

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);}}
整个代码都很简单,我也是做tv开发不久,所以会从最简单的写起!


更多相关文章

  1. Android动画介绍汇总
  2. TextView获取父控件的绘图状态
  3. Android应用程序开发期末大作业(1)
  4. Android(安卓)Gallery3d源码学习总结(二)——绘制流程drawThumbnai
  5. Android(安卓)使用自定义字体
  6. Android(安卓)中 ListView 控件的使用
  7. Android(安卓)首页Fragment切换常用姿势
  8. Android之回调函数
  9. 细说:AndroidStudio插件

随机推荐

  1. Android(安卓)匿名共享内存C接口分析
  2. Android源代码下载指南(图解)
  3. android的aidl机制案例
  4. Android(安卓)使用Vitamio打造自己的万能
  5. 【转】Ubuntu下Adb调试Android找不到设备
  6. Android(安卓)移植到高清机顶盒csm1201[
  7. Android(安卓)TV机顶盒开发简单介绍
  8. app测试中ios和Android的区别
  9. Android(安卓)使用LeakCanary 检测内存泄
  10. Google Android开发精华教程【转】