android菜单详解六:快捷键和菜单intent
来源:互联网 发布:em算法e步骤 编辑:程序博客网 时间:2024/05/05 18:24
快捷键
为了提高对选项菜单的操作速度,你可以在具有物理按键的设备上为菜单增加快捷键.快捷键可以对应键盘上的字母或数字.你需要做的是为<item>
元素指定属性android:alphabeticShortcut
和android:numericShortcut
的值.你也可以在代码中使用方法setAlphabeticShortcut(char)
和setNumericShortcut(char)
.来完成.快捷键并不是大小写敏感的.
例如,如果你把"s"键作为菜单项 "save" 的快捷键,那么当菜单打开时,用户按下了 "s" 键,"save"菜单项就被选择.
快捷键会以tip的方式出现在菜单项的名字的下方(除非菜单是图标菜单,它只能在用户按下"菜单"键时出现).
注:快捷键只能在有物理键盘的设备上起作用,并且不能用在上下文菜单上.
动态添加菜单intents
有时你可能希望通过一个菜单项使用Intent启动一个activiry(不论这个activity在你自己的程序中还是在另一个程序中 ).如果你知道了需要的Intent,你可以在响应对应菜单项的回调方法中执行Intent的startActivity()
方法完成.
然而,如果你不能确信用户设备上具有响应这个intent的程序,那么添加的菜单项可能成多余的.为了解决这个问题,Android 允许你在发现具有所有响应目标intent的activity时动态添加菜单项.
要跟据是否具有响应目标intent的Activity来添加菜单项,你需要:
- 定义一个具有类别
CATEGORY_ALTERNATIVE
和/或CATEGORY_SELECTED_ALTERNATIVE
的intent,当然还可以跟据需要添加其它类别. - 调用
Menu.addIntentOptions()
. Android会查找可以执行这个的程序然后把它添加到你的菜单上.
如此一来,如果没有满之intent的程序存在,则没有菜单项会添加.
注:CATEGORY_SELECTED_ALTERNATIVE
被用于处理屏幕上当前被选择的元素.所以,它只能用于在onCreateContextMenu()中被建的菜单.
例如:
@Overridepublic boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); // Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE. Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // Search and populate the menu with acceptable offering applications. menu.addIntentOptions( R.id.intent_group, // Menu group to which new items will be added 0, // Unique item ID (none) 0, // Order for the items (none) this.getComponentName(), // The current activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null); // Array of MenuItems that correlate to specific items (none) return true;}
每发现一个对应这个intent的activity,就会添加一个菜单项.将intent 过滤器中 android:label
的值作为菜单项的标题,将程序的图标作为菜单项的图标. 方法addIntentOptions()
返回被添加的菜单项的数目.
注:当调用 addIntentOptions()
时,会将参数menu group所指的group下的所有菜单项替换掉.
允许你的activity能被添加到其它菜单中
你也可以把你的activity的服务向其它程序提供.于是你的程序可以被其它程序的菜单所包含 (跟上一小节反过来了).
要想能被其它程序的菜单所包含,你需要定义一个intent 过滤器, 但这个过滤器必须在类别中包含 CATEGORY_ALTERNATIVE
和/或CATEGORY_SELECTED_ALTERNATIVE
,例如:
<intent-filter label="Resize Image"> ... <category android:name="android.intent.category.ALTERNATIVE" /> <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> ...</intent-filter>
- android菜单详解六:快捷键和菜单intent
- android菜单详解六:快捷键和菜单intent
- 【Android】添加菜单和监听菜单方法详解
- Android---菜单详解
- android菜单详解
- Android菜单详解
- Android菜单详解
- Android菜单详解
- android Menu菜单详解
- Android menu菜单详解
- android菜单详解
- android菜单详解
- Android菜单详解
- Android--菜单详解
- Android中的菜单详解
- 六.菜单
- 设置按钮和菜单快捷键
- android菜单详解二:选项菜单
- Android之PopuWindow简单学习
- 香港时间服务器
- Keil MDK编译器的数据类型定义
- O'Reilly Java系列书籍建议阅读顺序(转自蔡学庸老师)
- 传统的数据库操作和Hibernate数据库操作,事务的体现
- android菜单详解六:快捷键和菜单intent
- C++星火01_编译指令
- socket编程:SO_REUSEADDR例解
- Android 近百个项目的源代码,覆盖Android开发的每个领域
- 内容页访问母版页的属性及控件
- Linux上部署Cassandra环境(single-node cluster)
- DataList的用法
- 起始篇
- 设计模式星火01_单例模式