选择项目选择对话框中的菜单 [英] option item selection in Dialog menu

查看:98
本文介绍了选择项目选择对话框中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已运行到一些麻烦最近,我想我需要你的帮助:)。
我目前正在展现在对话框顶部的菜单,我知道这可能是更容易推出新的活动还没有这样做会compell我来存储/传递大量的数据。
我设法通过编写自定义对话框并重写oncreateOptionMenu方法来显示一个optionmenu。
我的问题是,我不能得到任何监听这些按钮,我试图重写onoptionitemselectedmethod但没有任何反应。
PS:我的对话几乎全屏幕,所以我不能看到活动对话框(我没有找到在上面方法的任何PUT)

我会很乐意尝试任何解决方案可以提供。

非常感谢

 公共布尔onCreateOptionsMenu(菜单菜单){
    super.onCreateOptionsMenu(菜单);
    menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
    menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
    返回true;
}
公共布尔onOptionsItemSelected(菜单项项){
    super.onOptionsItemSelected(项目);
    如果(item.getItemId()== 0){
        。getOwnerActivity()startActivity(新意图(C,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }
    其他
        如果(L→1)
            。getOwnerActivity()startActivity(新意图(C,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        其他
            解雇();
    返回true;
}


解决方案

也许这小片段从我的应用程序可以帮助你:

 私有静态最终诠释REFRESH_ID = Menu.FIRST + 1;公共布尔onCreateOptionsMenu(菜单菜单){
  布尔结果= super.onCreateOptionsMenu(菜单);
  menu.add(0,REFRESH_ID,0,R.string.menu_refresh).setIcon(R.drawable.and_refresh);
  返回结果;
}公共布尔onMenuItemSelected(INT FEATUREID,菜单项项){
  开关(item.getItemId()){
    案例REFRESH_ID:{
      //你想在这里什么!
      返回true;
    }
  }
  返回super.onMenuItemSelected(FEATUREID,项目);
}

正如你所看到的,我有我的菜单项的常量。这些项目得到Menu.First + N号整数。对于每一个项目,我算不起来。更简单的,然后改变它每次;)
而在onMenuItemSelected就可以轻​​松切换这些常数。
希望帮助!

I have been running into some troubles recently and I think I need your help :). I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data. I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method. My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens. Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)

I would be glad to try any solution you could provide.

Thanks a lot

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
    menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    if(item.getItemId()==0){
        getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }
    else
        if(l>1)
            getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        else
            dismiss();
    return true;
}

解决方案

Maybe this little snippet out of my app helps you:

private static final int REFRESH_ID = Menu.FIRST + 1;

public boolean onCreateOptionsMenu(Menu menu) {
  boolean result = super.onCreateOptionsMenu(menu);
  menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
  return result;
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
  switch (item.getItemId()) {
    case REFRESH_ID: {
      // Do whatever you want here!
      return true;  
    }
  }
  return super.onMenuItemSelected(featureId, item);
}

As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;) And in the onMenuItemSelected you can switch those constants easily. Hope that helps!

这篇关于选择项目选择对话框中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆