问题描述
在说明如何创建clicakble检查像下面的图片选项菜单任何教程方向任何人都可以点:
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:
我已经试过如下:
/** 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
编辑:我已经添加了对$ P $以下ppareOptionsMenu方式:
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
推荐答案
如果要动态更改选项菜单的状态,你需要使用上prepareMenu()
。在这种方法中,你可以做动态检查和更新你想要的任何东西。
祝你好运!结果
<一href=\"http://developer.android.com/reference/android/app/Activity.html#on$p$ppareOptionsMenu%28android.view.Menu%29\"相对=nofollow>文档
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
一些挖后,这看起来像一个自定义视图。我觉得你的图片均<一个href=\"http://android.git.kernel.org/?p=platform/packages/apps/Phone.git;a=blob;f=src/com/android/phone/InCallMenuView.java;h=560a082b6da4dd9879912ca7fe4e917682c80600;hb=HEAD\"相对=从这个code nofollow的>。
After some digging, this look like a custom view. I think your picture comes from this code.
这篇关于Android的 - 选项菜单上的辨认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!