Android onMenuItemClick接收到错误的项目

huangapple go评论87阅读模式
英文:

Android onMenuItemClick receiving wrong item

问题

我有一个菜单,当我创建它时,它可以正常弹出。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/day"
        android:title="Day" />
    <item
        android:id="@+id/week"
        android:title="Week" />
    <item
        android:id="@+id/month"
        android:title="Month" />
    <item
        android:id="@+id/year"
        android:title="Year" />
</menu>

出于某种原因,onMenuItemClick 只会在我点击任何项时返回 "Year"。这是一个非常奇怪的问题,我不知道为什么会发生。

public void showMenu(View v) {
    PopupMenu popup = new PopupMenu(getContext(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.date_range_menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.day:
                    ((MainActivity) requireActivity()).toast("Day");
                    changeDateRange(DateRange.DAY);
                case R.id.week:
                    ((MainActivity) requireActivity()).toast("Week");
                    changeDateRange(DateRange.WEEK);
                case R.id.month:
                    ((MainActivity) requireActivity()).toast("Month");
                    changeDateRange(DateRange.MONTH);
                case R.id.year:
                    ((MainActivity) requireActivity()).toast("Year");
                    changeDateRange(DateRange.YEAR);
                default:
                    return false;
            }
        }
    });
}
英文:

I have this menu that pops up fine when I create it.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item
        android:id=&quot;@+id/day&quot;
        android:title=&quot;Day&quot; /&gt;
    &lt;item
        android:id=&quot;@+id/week&quot;
        android:title=&quot;Week&quot; /&gt;
    &lt;item
        android:id=&quot;@+id/month&quot;
        android:title=&quot;Month&quot; /&gt;
    &lt;item
        android:id=&quot;@+id/year&quot;
        android:title=&quot;Year&quot; /&gt;
&lt;/menu&gt;

For some reason, the onMenuItemClick is only giving me "Year" no matter which item I click on. This is a really strange problem and I have no idea why it's happening.

public void showMenu(View v) {
	PopupMenu popup = new PopupMenu(getContext(), v);
	MenuInflater inflater = popup.getMenuInflater();
	inflater.inflate(R.menu.date_range_menu, popup.getMenu());
	popup.show();

	popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem item) {
			switch (item.getItemId()) {
				case R.id.day:
					((MainActivity) requireActivity()).toast(&quot;Day&quot;);
					changeDateRange(DateRange.DAY);
				case R.id.week:
					((MainActivity) requireActivity()).toast(&quot;Week&quot;);
					changeDateRange(DateRange.WEEK);
				case R.id.month:
					((MainActivity) requireActivity()).toast(&quot;Month&quot;);
					changeDateRange(DateRange.MONTH);
				case R.id.year:
					((MainActivity) requireActivity()).toast(&quot;Year&quot;);
					changeDateRange(DateRange.YEAR);
				default:
					return false;
			}
		}
	});
}

答案1

得分: 1

需要在每个case语句中添加break;关键字。

英文:

You need to add the break; keyword to each case.

huangapple
  • 本文由 发表于 2020年8月6日 02:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63271572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定