Android——可检查菜单项的问题

发布于 2024-11-04 23:26:10 字数 2273 浏览 1 评论 0原文

我已阅读 Android 开发人员页面上的说明,以获得可检查的菜单项:

http://developer.android.com/guide/topics/ui/menus.html

这是我的 xmlmenu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="all">
        <item android:id="@+id/regu"
              android:title="@string/Regulatory" />
        <item android:id="@+id/warn"
              android:title="@string/Warning" />
        <item android:id="@+id/temp"
              android:title="@string/Temporary" />
        <item android:id="@+id/bicy"
              android:title="@string/Bicycle" />
    </group>
</menu>

这是我的代码:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.regu:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
          return true;
      case R.id.warn:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
        return true;
      case R.id.temp:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
          return true;
      default:
        return super.onOptionsItemSelected(item);
      }
    }

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.app_menu, menu);
        return true;
    }

问题是当我单击一项时,菜单项消失了。它不必保持可见才能检查其他菜单项吗?

有什么想法吗?

问候

I have read the instructions at the android developers page's in order to get the Checkable menu items:

http://developer.android.com/guide/topics/ui/menus.html

this is my xmlmenu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="all">
        <item android:id="@+id/regu"
              android:title="@string/Regulatory" />
        <item android:id="@+id/warn"
              android:title="@string/Warning" />
        <item android:id="@+id/temp"
              android:title="@string/Temporary" />
        <item android:id="@+id/bicy"
              android:title="@string/Bicycle" />
    </group>
</menu>

And here is my code:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.regu:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
          return true;
      case R.id.warn:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
        return true;
      case R.id.temp:
          if (item.isChecked())
          {
              item.setChecked(false);
              currAvailableOptions++;
          }
          else if(0 != currAvailableOptions)
          {
                  item.setChecked(true);
                  currAvailableOptions--;
          }
          return true;
      default:
        return super.onOptionsItemSelected(item);
      }
    }

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.app_menu, menu);
        return true;
    }

The problem is when I clicked one item, the menu item disappeared. It wouldn't have to stay visible in order to check other menu items?

Any idea?

Greetings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷又爱笑 2024-11-11 23:26:10

可检查的项目仅出现在子菜单或上下文菜单中。

对于子菜单,它们(Google)意味着:

子菜单 当用户选择菜单项时出现的浮动菜单项列表
触摸包含嵌套菜单的菜单项。

由于您的菜单项不是子菜单项,因此它不起作用

Checkable items appear only in submenus or context menus.

And with submenu they (Google) means:

Submenu A floating list of menu items that appears when the user
touches a menu item that contains a nested menu.

Since your menu items are not submenu items, it will not work

小红帽 2024-11-11 23:26:10

我知道这不是对您问题的直接答案,但请考虑以下代码而不是您的开关,它可能会帮助您找到问题。

public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.regu:
  case R.id.warn:
  case R.id.temp:
      if (item.isChecked())
           currAvailableOptions++;
      else if(currAvailableOptions != 0)
           currAvailableOptions--;
      item.setChecked(!item.isChecked());
      return true;
  default:
      return super.onOptionsItemSelected(item);
  }
}

I know this is not a direct answer to your question but please consider the following code instead of your switch, it might help you find the problem.

public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.regu:
  case R.id.warn:
  case R.id.temp:
      if (item.isChecked())
           currAvailableOptions++;
      else if(currAvailableOptions != 0)
           currAvailableOptions--;
      item.setChecked(!item.isChecked());
      return true;
  default:
      return super.onOptionsItemSelected(item);
  }
}
挽袖吟 2024-11-11 23:26:10

什么是 currAvailableOptions?我看了你链接的文章,里面没有任何内容。看起来您需要做的就是检查:

   if (item.isChecked())
      item.setChecked(false);
   else
      item.setChecked(true);

或者至少教程是这么说的。也许你应该再读一遍?希望这有帮助。

What is currAvailableOptions? I looked at the article you linked to and there wasn't anything about that in there. It would seem that all you need to do is check:

   if (item.isChecked())
      item.setChecked(false);
   else
      item.setChecked(true);

or at least that's what the tutorial says. Perhaps you should give it another read? Hope this helps.

七分※倦醒 2024-11-11 23:26:10

您可能应该在每个 case: 之后添加 break; 语句

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
        item.setChecked(!item.isChecked());
        break;
    case R.id.item2:
        item.setChecked(!item.isChecked());
        break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

You should probably add break; statements after each case:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
        item.setChecked(!item.isChecked());
        break;
    case R.id.item2:
        item.setChecked(!item.isChecked());
        break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文