首页 > 代码库 > 关于机顶盒焦点的获取,及事件响应!!

关于机顶盒焦点的获取,及事件响应!!

记得刚做机顶盒开发的时候</span>,真的把我坑惨了,网上又找都不什么资料,所以只能自己慢慢摸索了。在其中也是学到了一些知识,当然也遇到过许多的问题,so。。。就希望把这些经验分享给其他刚开始步入机顶盒开发的程序猿。。。。。</span>

做机顶盒这块了,最麻烦的就是这个焦点问题了,因为机顶盒都是用遥控操作的,所以在界面上都需要有一个用户操作的焦点!!!下面就带大家写一个在机顶盒中经常用到的界面切换的程序。。。

程序有四个界面,按遥控的左右键,能切换到不同界面,下面是布局。(注:布局最好是新建一个layout-land文件夹存放)


activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/launch_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioGroup
        android:id="@+id/title_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dip"
        android:orientation="horizontal"
        android:paddingLeft="120dip"
        android:paddingRight="60dip" >

        <RadioButton
            android:id="@+id/l1"
            style="@style/TitleButton"
            android:layout_width="180dip"
            android:layout_height="wrap_content"
            android:button="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:nextFocusLeft="@+id/tv_show"
            android:text="第一" />

        <RadioButton
            android:id="@+id/l2"
            style="@style/TitleButton"
            android:layout_width="180dip"
            android:layout_height="wrap_content"
            android:button="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="第二" />

        <RadioButton
            android:id="@+id/l3"
            style="@style/TitleButton"
            android:layout_width="180dip"
            android:layout_height="wrap_content"
            android:button="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="第三" />

        <RadioButton
            android:id="@+id/l4"
            style="@style/TitleButton"
            android:layout_width="180dip"
            android:layout_height="wrap_content"
            android:button="@null"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:nextFocusRight="@+id/settings"
            android:text="第四" />
    </RadioGroup>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_layout_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp" />

</RelativeLayout>

RadioGroup的样式:定义了选中和未选中radiogroup的背景及颜色的改变。

 <style name="TitleButton">
        <item name="android:background">@drawable/title_button_selector</item>
        <item name="android:textColor">@drawable/title_textcolor_selector</item>
        <item name="android:textSize">25sp</item>
    </style>


layout1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="这是第一个界面" />

</LinearLayout>
其他的三个布局文件都差不多,就不一一列举了。。。

接下来看 view1的编写:

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class View1 extends LinearLayout {
	private Context mContext;

	public View1(Context context) {
		super(context);
		mContext = context;
	}

	public void initView() {
		setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		setGravity(Gravity.CENTER_HORIZONTAL);
		View root = LayoutInflater.from(mContext).inflate(R.layout.layout1,
				null);
		addView(root);
	}
}
这里我继承了LinerrLayout,是为了能在mainactivity把我的view填充进去,其他的都比较简单了,后面的几个view也是这样,只不过就是布局不一样而已。

然后是我们的MainActivity:

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
	private View1 v1;
	private View2 v2;
	private View3 v3;
	private View4 v4;
	private ArrayList<View> pages = new ArrayList<View>();
	private ViewPager centerPager;
	private RadioGroup titleGroup;// 标签组

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		v1 = new View1(this);
		v2 = new View2(this);
		v3 = new View3(this);
		v4 = new View4(this);

		pages.add(v1);
		pages.add(v2);
		pages.add(v3);
		pages.add(v4); // 添加页面
		initView();
		initListener();
	}

	private void initView() {
		titleGroup = (RadioGroup) findViewById(R.id.title_group);
		titleGroup.check(R.id.l1);
		centerPager = (ViewPager) findViewById(R.id.main_layout_pager);
		centerPager.setAdapter(new AllPagesAdapter(pages));
		// 当前显示第一页
		centerPager.setCurrentItem(0);
		v1.initView();//加载页面
		v2.initView();
		v3.initView();
		v4.initView();
	}
<span style="white-space:pre">	</span>//对radiogroup的焦点监听,进行页面的切换
	public void initListener() {
		OnFocusChangeListener focusListener = new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					int position = (Integer) v.getTag();
					centerPager.setCurrentItem(position, true);
				}
			}
		};
		for (int i = 0; i < titleGroup.getChildCount(); i++) {
			View view = titleGroup.getChildAt(i);
			view.setTag(i);
			view.setOnFocusChangeListener(focusListener);
		}

		centerPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				if (arg0 < titleGroup.getChildCount()) {
					((RadioButton) titleGroup.getChildAt(arg0))
							.setChecked(true);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}
}
适配器代码:

import java.util.ArrayList;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public class AllPagesAdapter extends PagerAdapter {
	private ArrayList<View> views;

	public AllPagesAdapter(ArrayList<View> views) {
		super();
		this.views = views;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {

	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View child = views.get(position);
		container.addView(child);
		return child;
	}

	@Override
	public int getCount() {
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	public void dataChanged(ArrayList<View> views) {
		this.views = views;
		notifyDataSetChanged();
	}
}
代码都比较简单,就到这里了,以后还会继续编写一些有关机顶盒的开发的程序。。。。看下效果图:








关于机顶盒焦点的获取,及事件响应!!