onMenuItemClick 切换案例选择多个案例
onMenuItemClick switch case selecting multiple cases
出于某种原因,我的 switch case 是 selecting 多个 case。
假设我单击排序 -> 按名称,它 selects 排序 -> 按名称,排序 -> 按点和统计信息(如下所示)。
这不是第一次发生,它也发生在代码的另一部分,我简单地将它切换为“如果”并忘记了它。为什么他会 select 点击多个值?为什么随机值?
开关盒功能:
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.optionsMenu_saveListDevice:
Toast.makeText(this, "saveDevice", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_saveListServer:
Toast.makeText(this, "saveServer", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_about:
Toast.makeText(this, "about", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByGrade:
Toast.makeText(this, "sortGrade", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByName:
Toast.makeText(this, "sortName", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByPoints:
Toast.makeText(this, "sortPoints", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_statistics:
Toast.makeText(this, "statistics", Toast.LENGTH_SHORT).show();
default:
return false;
}
显示弹出窗口:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
popup.inflate(R.menu.popup_menu);
popup.show();
}
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/optionsMenu_sort"
android:title="Sort">
<menu>
<item
android:id="@+id/optionsMenu_sortByName"
android:title="By name" />
<item
android:id="@+id/optionsMenu_sortByGrade"
android:title="By grade" />
<item
android:id="@+id/optionsMenu_sortByPoints"
android:title="By points" />
</menu>
</item>
<item
android:id="@+id/optionsMenu_save"
android:title="Save">
<menu>
<item
android:id="@+id/optionsMenu_saveListDevice"
android:title="On device" />
<item
android:id="@+id/optionsMenu_saveListServer"
android:title="On server" />
</menu>
</item>
<item
android:id="@+id/optionsMenu_statistics"
android:title="Statistics" />
<item
android:id="@+id/optionsMenu_about"
android:title="About" />
每次祝酒后你应该放一个break
。
请参阅 Tutorialspoint.com
中的示例
public class Test {
public static void main(String args[]) {
// char grade = args[0].charAt(0);
char grade = 'C';
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
出于某种原因,我的 switch case 是 selecting 多个 case。
假设我单击排序 -> 按名称,它 selects 排序 -> 按名称,排序 -> 按点和统计信息(如下所示)。
这不是第一次发生,它也发生在代码的另一部分,我简单地将它切换为“如果”并忘记了它。为什么他会 select 点击多个值?为什么随机值?
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.optionsMenu_saveListDevice:
Toast.makeText(this, "saveDevice", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_saveListServer:
Toast.makeText(this, "saveServer", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_about:
Toast.makeText(this, "about", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByGrade:
Toast.makeText(this, "sortGrade", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByName:
Toast.makeText(this, "sortName", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_sortByPoints:
Toast.makeText(this, "sortPoints", Toast.LENGTH_SHORT).show();
case R.id.optionsMenu_statistics:
Toast.makeText(this, "statistics", Toast.LENGTH_SHORT).show();
default:
return false;
}
显示弹出窗口:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
popup.inflate(R.menu.popup_menu);
popup.show();
}
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/optionsMenu_sort"
android:title="Sort">
<menu>
<item
android:id="@+id/optionsMenu_sortByName"
android:title="By name" />
<item
android:id="@+id/optionsMenu_sortByGrade"
android:title="By grade" />
<item
android:id="@+id/optionsMenu_sortByPoints"
android:title="By points" />
</menu>
</item>
<item
android:id="@+id/optionsMenu_save"
android:title="Save">
<menu>
<item
android:id="@+id/optionsMenu_saveListDevice"
android:title="On device" />
<item
android:id="@+id/optionsMenu_saveListServer"
android:title="On server" />
</menu>
</item>
<item
android:id="@+id/optionsMenu_statistics"
android:title="Statistics" />
<item
android:id="@+id/optionsMenu_about"
android:title="About" />
每次祝酒后你应该放一个break
。
请参阅 Tutorialspoint.com
中的示例public class Test {
public static void main(String args[]) {
// char grade = args[0].charAt(0);
char grade = 'C';
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}