这个地方的“default”,我觉得比较好。
package cn.fansunion.menu; import cn.fansunion.R; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; /** * ①:直接去覆盖public boolean onCreateOptionsMenu(Menu menu) { code......}, * 这个方法,需要注意的是,这个方法如果覆盖的了,只会被创建一次,也就是说, 选项菜单只会去被实例化一次,之后就不会被去调用了 * ②:调用Menu中的add()方法,来添加每一个菜单选项, add(groupId, itemId, order, titleRes) group: * 选项组号,一般都设置成0就OK啦 itenId: 选项的Id 很重要 order:顺序,一般来说都设置0就行了 titelRes: 选项的标题名字 * ③:当我们去点击某项的选项的时候,覆盖重写onOptionsItemSelected(MenuItem item)这个方法去实现点击事件. */ public class OptionMenuActivity extends Activity { // 点击菜单选项的常量Id private static final int MENU_ONE = 1; private static final int MENU_TWO = 2; private static final int MENU_THREE = 3; private int MENU_SUB = 11; private static final int GROUP_ZERO = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.option_menu); } /** * @param group * : 选项组号,一般都设置成0就OK啦 * @param itenId * : 选项的Id 很重要 * @param order * :顺序,一般来说都设置0就行了 * @param titelRes * : 选项的标题名字 */ public boolean onCreateOptionsMenu(Menu menu) { menu.add(GROUP_ZERO, MENU_ONE, GROUP_ZERO, "小雷"); // 一个menu可以包括多个子菜单 SubMenu subMenu=menu.addSubMenu(GROUP_ZERO, MENU_SUB, GROUP_ZERO, "小雷兴趣"); subMenu.add(GROUP_ZERO, MENU_TWO, GROUP_ZERO, "互联网技术"); subMenu.add(GROUP_ZERO, MENU_THREE, GROUP_ZERO, "投资理财"); return true; } /** * * @param item * .getItemId() 获取被点击的Id */ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ONE: Toast.makeText(this, "你点击了Menu1", Toast.LENGTH_LONG).show(); break; case MENU_TWO: Toast.makeText(this, "你点击了Menu2", Toast.LENGTH_LONG).show(); break; case MENU_THREE: Toast.makeText(this, "你点击了Menu3", Toast.LENGTH_LONG).show(); break; default: return super.onOptionsItemSelected(item); } return true; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="FansUnion,Android Demo "/> </LinearLayout>
package cn.fansunion.menu; import cn.fansunion.R; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ContextMenuActivity extends Activity { private static final int GROUP = 0; private static final int MENU_ONE = 1; private static final int MENU_TWO = 2; private static final int MENU_THREE = 3; private TextView contextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.context_menu); contextView = (TextView) this.findViewById(R.id.contextMenu); registerForContextMenu(contextView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(GROUP, MENU_ONE, 0, "小雷FansUnion"); menu.add(GROUP, MENU_TWO, 0, "互联网技术"); menu.add(GROUP, MENU_THREE, 0, "投资理财"); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ONE: Toast.makeText(this, "你 点击的是Menu1", Toast.LENGTH_LONG).show(); contextView.setText("你点击的是Menu1"); break; case MENU_TWO: Toast.makeText(this, "你点击的是Menu2", Toast.LENGTH_LONG).show(); contextView.setText("你点击的是Menu2"); break; case MENU_THREE: Toast.makeText(this, "你点击的是Menu3", Toast.LENGTH_LONG).show(); contextView.setText("你点击的是Menu3"); break; } return true; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/contextMenu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="FansUnion,ContextMenuDemo "/> </LinearLayout>