展会信息港展会大全

android菜单详解六:快捷键和菜单intent
来源:互联网   发布日期:2016-01-14 09:43:59   浏览:1432次  

导读:快捷键为了提高对选项菜单的操作速度,你可以在具有物理按键的设备上为菜单增加快捷键.快捷键可以对应键盘上的字母或数字.你需要做的是为<item>元素指定属性android:alphabetic...

快捷键

为了提高对选项菜单的操作速度,你可以在具有物理按键的设备上为菜单增加快捷键.快捷键可以对应键盘上的字母或数字.你需要做的是为<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()中被建的菜单.

例如:

@Override

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

}

@Override

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

<intent-filter label="Resize Image">

...

<category android:name="android.intent.category.ALTERNATIVE" />

<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />

...

</intent-filter>

摘自 nkmnkm的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港