Android - 选项菜单上的可检查按钮
任何人都可以指出任何教程的方向,显示如何创建带有可点击检查的选项菜单,如下图所示:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
我已尝试如下:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
但这不起作用,它只在按钮上提供文本选项菜单
编辑:我添加了以下 onPrepareOptionsMenu 方法:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
但是,我得到的结果与上图所示相同,只是文本,没有检查灯
Can anyone point in the direction of any tutorials that show how to create an options menu with clicakble checks like in the picture below:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
I have tried as follows:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
But this doesn't work it only gives me text on the buttons on the options menu
EDIT: I have added the following onPrepareOptionsMenu method:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
However I get the same outcome just text and no check light as in the picture above
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想动态更改选项菜单的状态,则需要使用
onPrepareMenu()
。在此方法中,您可以进行动态检查并更新任何您想要的内容。祝你好运!!
文档
经过一番挖掘,这看起来像一个自定义视图。我认为你的图片来自 来自此代码。
If you want to change dynamically the state of your Option Menu, you need to use
onPrepareMenu()
. In this method, you can do dynamic checks and update anything you want.Good luck!!
documentation
After some digging, this look like a custom view. I think your picture comes from this code.
这是一个老问题,但我遇到了同样的问题,并进行了大量搜索以找到上面显示的这样的选项菜单。我在 http://www.codeproject.com 上找到了一个教程,并对其进行了一些修改。也许它不是专业程序员代码,但它对我有用。请参阅我在谷歌网站上的(安排不佳的)网页上的修改(我在该网站上也有一个小教程):
https://sites.google.com/site/opiatefuchs/android-code-examples
此代码源自代码项目网页上的 wjfrancis(许多道具):
http://www.codeproject.com/Articles/173121/Android-Menus-My-方式
我刚刚修改了它,如果有人有任何想法来改进此代码,我会很高兴。但就目前而言,它有效。
This is an old question, but I had the same problem and searched a lot to find such an optionsMenu shown above. I found a tutorial on http://www.codeproject.com and modified it a little bit. Maybe it is not a profi-programmers-code, but it works for me. See my modifications at my (poor arranged) web page on google sites (I got an little tutorial too on this site):
https://sites.google.com/site/opiatefuchs/android-code-examples
This code is original from wjfrancis on the Code Project web page (many props):
http://www.codeproject.com/Articles/173121/Android-Menus-My-Way
I just modified it and would be glad if somebody got any ideas to improve this code. But for now, it works.