安卓-----------使用方向键切换焦点
来源:互联网 发布:淘宝搞笑最牛评论 编辑:程序博客网 时间:2024/06/16 11:07
使用方向键在不同的控件中切换焦点,开始使用textview做为控件时发现不行,改为button控件后,正常使用。
这里主要是布局文件,activity使用创建默认代码即可
activity_main.xml文件:
<RelativeLayout 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"> <Button android:id="@+id/text1" android:layout_centerHorizontal="true" android:layout_width="50dp" android:layout_height="50dp" android:nextFocusLeft="@+id/text2" android:nextFocusDown="@+id/text4" android:nextFocusRight="@+id/text3" android:nextFocusUp="@+id/text4" android:background="@drawable/bar_bg_select" android:text="1" android:focusableInTouchMode="true"> <requestFocus /> </Button> <Button android:id="@+id/text2" android:layout_below="@+id/text1" android:layout_alignParentLeft="true" android:layout_width="50dp" android:layout_height="50dp" android:nextFocusLeft="@+id/text3" android:nextFocusDown="@+id/text4" android:nextFocusRight="@+id/text3" android:nextFocusUp="@+id/text1" android:background="@drawable/bar_bg_select" android:text="2" > </Button> <Button android:id="@+id/text3" android:layout_below="@+id/text1" android:layout_alignParentRight="true" android:layout_width="50dp" android:layout_height="50dp" android:nextFocusLeft="@+id/text2" android:nextFocusDown="@+id/text4" android:nextFocusRight="@+id/text2" android:nextFocusUp="@+id/text1" android:background="@drawable/bar_bg_select" android:text="3" > </Button> <Button android:id="@+id/text4" android:layout_below="@+id/text2" android:layout_centerHorizontal="true" android:layout_width="50dp" android:layout_height="50dp" android:nextFocusLeft="@+id/text2" android:nextFocusDown="@+id/text1" android:nextFocusRight="@+id/text3" android:nextFocusUp="@+id/text1" android:background="@drawable/bar_bg_select" android:text="4" > </Button></RelativeLayout>使用android:nextfocusup等方法来选择下一个控件,另外建一个选择器,用来实现选中焦点和未选中焦点时控件的样式。这里的requestFocus是用来获取初始焦点的。
bar_bg_select.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/bar_bg_normal" /> <item android:state_focused="true" android:drawable="@drawable/bar_bg_focus" /> </selector>
bar_bg_normal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 背景色 --> <solid android:color="#FFE4B5"/></shape>
bar_bg_focus.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 背景色 --> <solid android:color="#FFE4B5"/> <!-- 边框色 --> <stroke android:width="1dip" android:color="#81CE47" /></shape>
以上,就可实现用方向键来控制在控件间的焦点切换。
0 0
- 安卓-----------使用方向键切换焦点
- 使用方向键切换INPUT焦点之左右键补遗
- 在minimo中用方向键切换焦点
- Tab键、方向键切换页面控件焦点
- 使用方向键控制table中文本框焦点
- wap 1.2在PPC上不支持方向键切换焦点
- Android RecyclerView中item焦点乱跳问题(适用于PDA以及TV等带方向键的安卓设备)
- Js实现类似Excel工作表用方向键切换输入框焦点
- WPF DataGrid分页切换时第一行获得焦点,键盘UP、Down方向键可上下移动
- 安卓TimePicker在当前页面通过按键事件切换焦点的方法
- 安卓·使用intent切换Activity
- webkit linux 方向键移动焦点
- javascript 焦点切换及history的使用
- 在.NET中使用回车切换焦点
- 使用myfocus实现网页焦点切换效
- 安卓 切换页面
- 安卓主题切换
- 安卓控件文字颜色切换-ColorStateList的使用
- Android颤动动画效果
- 对眼睛最舒适的颜色
- myeclipse优化方案 myeclipse 10 优化
- 主动学习和半监督学习(转)
- 环境: ubuntu 用cmake 从Ogre源码编译Ogre SDK
- 安卓-----------使用方向键切换焦点
- 锐捷RG-WALL60防火墙配置详解
- Processes and Threads
- 百度地图API使用讲解
- poj 1700 Crossing River
- onInterceptTouchEvent和onTouchEvent调用时序 责任链 模式。
- 使用uiautomator 做UI测试
- WIN32和Kernel直接读写硬盘扇区
- 【翻译】动态图像监测开源代码 motion 学习-----Motion Guide - Basic Features(motion的基本特性)(二)