Android 中menu可分为两种,分别为options menu(menu键产生), context menu(长按屏幕产生)下面举个例子; 1,options menu(menu键产生), [java] view plaincopyprint? @Override public boolean onCreateOptionsMenu(Menu menu) { m=menu; menu.add(0, Menu.FIRST, 0, "1st"); menu.add(0, Menu.FIRST+1, 0, "2st"); menu.add(0, Menu.FIRST+2, 0, "3st"); menu.add(0, Menu.FIRST+3, 0, "4st"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; case 2: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; case 3: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; case 4: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; } return super.onOptionsItemSelected(item); } 注意:public boolean onCreateOptionsMenu(Menu menu)方法只被系统调用一次,如需要动态更改菜单内容还需重写onPrepareOptionsMenu(Menu menu)方法实现 [java] view plaincopyprint? Menu m=null; int count=0; @Override public boolean onPrepareOptionsMenu(Menu menu) { if(count>0){ if(count%2==0){ menu.removeGroup(1); }else{ menu.add(1, Menu.FIRST, 0, "5st"); menu.add(1, Menu.FIRST+1, 0, "6st"); } } count++; return super.onPrepareOptionsMenu(menu); } 2,context menu(长按屏幕产生) [java] view plaincopyprint? @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; case 2: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, Menu.FIRST, 0, "1st"); menu.add(0, Menu.FIRST+1, 0, "2st"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; case 2: Toast.makeText(this, "you select"+item.getItemId(), 500).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, Menu.FIRST, 0, "1st"); menu.add(0, Menu.FIRST+1, 0, "2st"); super.onCreateContextMenu(menu, v, menuInfo); } 还有一步,与View进行绑定; [java] view plaincopyprint? /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); MyView my=new MyView(this); setContentView(my); registerForContextMenu(my); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); MyView my=new MyView(this); setContentView(my); registerForContextMenu(my); } 以上是通过代码实现,还可以通过xml文件实现 menu.xml 内容如下; [java] view plaincopyprint? <menu xmlns:android="http://schemas./apk/res/android"> <item android:id="@+id/fourth_item" android:orderInCategory="3" android:title="Fourth" /> <item android:id="@+id/third_item" android:orderInCategory="2" android:title="Third" /> <item android:id="@+id/dive" android:orderInCategory="1" android:title="Second" /> <item android:id="@+id/jump" android:orderInCategory="0" android:title="First" /> </menu><menu xmlns:android="http://schemas./apk/res/android"> <item android:id="@+id/fourth_item" android:orderInCategory="3" android:title="Fourth" /> <item android:id="@+id/third_item" android:orderInCategory="2" android:title="Third" /> <item android:id="@+id/dive" android:orderInCategory="1" android:title="Second" /> <item android:id="@+id/jump" android:orderInCategory="0" android:title="First" /> </menu>
[java] view plaincopyprint? @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the currently selected menu XML resource. MenuInflater inflater = getMenuInflater(); inflater.inflate(sMenuExampleResources[R.m], menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.jump: Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show(); return true; case R.id.dive: Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show(); return false; }
|
|
来自: androidHung > 《android》