android tv开发基础知识焦点处理
2016-05-01 16:26
573 查看
机顶盒开发和手机开发最大的区别就在于view的事件处理上了,机顶盒开发没什么touch事件,而机顶盒是用遥控器去操作界面交互的,而手机是用手去操作界面的,所以我们用遥控器去操作界面表示你焦点在哪个view上,一般都在该view上把背景设置为和其他的view有区别或者是放大的效果,这样体验起来效果就更好,现在写个demo体验下,
这上面是10个button,现在看下使用遥控器操作的效果,
![](http://img.blog.csdn.net/20160501115402074?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
哪个button有焦点,但是系统给它设置的背景色很浅,所以我们一般都是自己去设置当哪个view有焦点的时候,view有setOnClickListener点击事件,当然也给我们设置了获取焦点的回调,setOnFocusChangeListener就是当view有焦点时候回调,
public void onFocusChange(View v, boolean hasFocus) {
}
v表示你当前哪个view有焦点,
hasFocus返回true表示v获取到焦点,返回false表示该view没有焦点,所以当view有焦点设置其背景一般就在这个方法,这个在机顶盒开发很普遍的做法,
还有就是遥控器有左,右,上,下四个键,这个就要涉及到view的setOnKeyListener监听了,遥控器有down和up事件,对应的就是KeyEvent.KEYCODE_DPAD_DOWN,KeyEvent.KEYCODE_DPAD_UP,KeyEvent.KEYCODE_DPAD_LEFT,KeyEvent.KEYCODE_DPAD_RIGHT,
button1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.e(TAG,"左----------");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e(TAG,"右----------");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.e(TAG,"上----------");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.e(TAG,"下----------");
break;
}
}
return false;
}
});
这个onKey()方法有一个boolean的返回值,上面的如果你上,下,左,右返回true的话就表示屏蔽了系统默认的处理方式,而右你自己去处理,一旦你返回true,界面上那么view就没有焦点了,可以调用isFocused()这个方法 打印它的返回值就知道它返回false,就表示view没有焦点了,如果这个时候要让view有焦点就必须调用requestFocus让它获取到焦点
button1.setOnKeyListener(new OnKeyListener() {
@Override
public 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:
button2.requestFocus();
return true;
}
}
return false;
}
});
button2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button1.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button3.requestFocus();
return true;
}
}
return false;
}
});
button3.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button2.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button4.requestFocus();
return true;
}
}
return false;
}
});
button4.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button3.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button5.requestFocus();
return true;
}
}
return false;
}
});
button5.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button4.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
}
return false;
}
});
这个是屏蔽了系统的处理方式,自己处理的效果,
<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" > <LinearLayout android:layout_width="600dp" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="400dp" android:layout_height="wrap_content" android:text="button1" /> <Button android:id="@+id/button2" android:layout_width="400dp" android:layout_height="wrap_content" android:text="button2" /> <Button android:id="@+id/button3" android:layout_width="400dp" android:layout_height="wrap_content" android:text="button3" /> <Button android:id="@+id/button4" android:layout_width="400dp" android:layout_height="wrap_content" android:text="button4" /> <Button android:id="@+id/button5" android:layout_width="400dp" android:layout_height="wrap_content" android:text="button5" /> </LinearLayout> <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>
这上面是10个button,现在看下使用遥控器操作的效果,
哪个button有焦点,但是系统给它设置的背景色很浅,所以我们一般都是自己去设置当哪个view有焦点的时候,view有setOnClickListener点击事件,当然也给我们设置了获取焦点的回调,setOnFocusChangeListener就是当view有焦点时候回调,
public void onFocusChange(View v, boolean hasFocus) {
}
v表示你当前哪个view有焦点,
hasFocus返回true表示v获取到焦点,返回false表示该view没有焦点,所以当view有焦点设置其背景一般就在这个方法,这个在机顶盒开发很普遍的做法,
@Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ setViewNormalBg(); switch (v.getId()) { case R.id.button1: setViewFouceBg(button1); break; case R.id.button2: setViewFouceBg(button2); break; case R.id.button3: setViewFouceBg(button3); break; case R.id.button4: setViewFouceBg(button4); break; case R.id.button5: setViewFouceBg(button5); break; 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; } } } public void setViewFouceBg(View view){ view.setBackgroundColor(Color.RED); } public void setViewNormalBg(){ button1.setBackgroundColor(Color.TRANSPARENT); button2.setBackgroundColor(Color.TRANSPARENT); button3.setBackgroundColor(Color.TRANSPARENT); button4.setBackgroundColor(Color.TRANSPARENT); button5.setBackgroundColor(Color.TRANSPARENT); button6.setBackgroundColor(Color.TRANSPARENT); button7.setBackgroundColor(Color.TRANSPARENT); button8.setBackgroundColor(Color.TRANSPARENT); button9.setBackgroundColor(Color.TRANSPARENT); button10.setBackgroundColor(Color.TRANSPARENT); }当button有焦点时背景设置为红色,其他没有焦点的view背景色就是透明的,这样用户一看就知道点击到哪个view了,
还有就是遥控器有左,右,上,下四个键,这个就要涉及到view的setOnKeyListener监听了,遥控器有down和up事件,对应的就是KeyEvent.KEYCODE_DPAD_DOWN,KeyEvent.KEYCODE_DPAD_UP,KeyEvent.KEYCODE_DPAD_LEFT,KeyEvent.KEYCODE_DPAD_RIGHT,
button1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.e(TAG,"左----------");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e(TAG,"右----------");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.e(TAG,"上----------");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.e(TAG,"下----------");
break;
}
}
return false;
}
});
这个onKey()方法有一个boolean的返回值,上面的如果你上,下,左,右返回true的话就表示屏蔽了系统默认的处理方式,而右你自己去处理,一旦你返回true,界面上那么view就没有焦点了,可以调用isFocused()这个方法 打印它的返回值就知道它返回false,就表示view没有焦点了,如果这个时候要让view有焦点就必须调用requestFocus让它获取到焦点
button1.setOnKeyListener(new OnKeyListener() {
@Override
public 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:
button2.requestFocus();
return true;
}
}
return false;
}
});
button2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button1.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button3.requestFocus();
return true;
}
}
return false;
}
});
button3.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button2.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button4.requestFocus();
return true;
}
}
return false;
}
});
button4.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button3.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
button5.requestFocus();
return true;
}
}
return false;
}
});
button5.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
button4.requestFocus();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
}
return false;
}
});
这个是屏蔽了系统的处理方式,自己处理的效果,
相关文章推荐
- android shape使用总结
- 在Ubuntu上搭建Android开发环境(Android Studio)
- android 5.x—Palette图片取色调
- android中进程优先级问题
- Android学习笔记4:探究活动1
- Android基础:动画三:属性动画
- Android学习笔记3:使用日志工具
- Android学习笔记1:创建Hello World项目
- android-----Activity生命周期
- Android之XmlSerializer生成xml文件
- Android中显示圆形头像(读书笔记)
- android中ListView的基本组件及用法
- Android 在开发中使用单元测试
- cordova-plugin-local-notifications发送Android本地消息
- Android中自定义EditText控件(自带清除功能等)
- android之MTP框架和流程分析
- Android改变Activity切换时的动画
- 企业邮箱在Android(安卓)系统手机上POP3/IMAP协议的设置方法
- 深入探索AsyncTask
- Gradle DSL method not found android()解决方法