60 likes | 372 Views
Menu 設定方法. OptionMenu- 直接設定. public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.activity_main, menu); menu.add(1,1,0," 多媒體播放 "); menu.add(1,2,1," 定位系統 "); menu.add(1,3,2," 影片播放 ");
E N D
OptionMenu-直接設定 • public boolean onCreateOptionsMenu(Menu menu) { • // Inflate the menu; this adds items to the action bar if it is present. • //getMenuInflater().inflate(R.menu.activity_main, menu); • menu.add(1,1,0,"多媒體播放"); • menu.add(1,2,1,"定位系統"); • menu.add(1,3,2,"影片播放"); • menu.add(1,4,3,"網路瀏覽"); • menu.add(1,5,4,"地圖顯示"); • menu.add(1,6,5,"天氣預報"); • menu.add(1,7,6,"行事錄"); • return true; • }
點擊啟動 • public boolean onOptionsItemSelected(MenuItem item) { • switch(item.getItemId()) { • case 1: • Show1(); • return true; • case 2: • Show2(); • return true; • case 3:/case 4:/case 5:/case 6: • case 7: • Show3(); • return true; • } • return super.onOptionsItemSelected(item); • }
ContextMenu • public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { • super.onCreateContextMenu(menu, v, menuInfo); • menu.add(1,1,0,”Edit"); • menu.add(1,2,1,“Save"); • } • public boolean onContextItemSelected(MenuItem item) { • switch (item.getItemId()) { • case 1: • Show1(); • return true; • case 2: • Show2(); • return true; • default: • return super.onContextItemSelected(item); • } • }
註冊 • TextView textView=(TextView)findViewById(R.id.textview1); • registerForContextMenu(textView);
Sub Menu • public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { • super.onCreateContextMenu(menu, v, menuInfo); • SubMenu editMenu=menu.addSubMenu(1,1,0,"編輯"); • SubMenu checkMenu=menu.addSubMenu(1,2,0,"檢查"); • editMenu.add(1,3,0,"編輯檔案"); • editMenu.add(1,4,1,"編輯資料表"); • checkMenu.add(1,5,0,"檢查有效日期"); • checkMenu.add(1,6,1,"檢查授權"); • checkMenu.add(1,7,2,"檢查預算"); • }