android UI menu code

目前还没有整理,只是把代码弄上来。

package com.mediatek.uidemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.menu);
  Button menuButton1 = (Button) findViewById(R.id.contextMenuButton1);
  this.registerForContextMenu(menuButton1);
  Button menuButton2 = (Button) findViewById(R.id.contextMenuButton2);
  this.registerForContextMenu(menuButton2);
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  
  menu.add(1, 1, 1, "111first").setOnMenuItemClickListener(new MenuItemClickListener());
  menu.add(1, 2, 2, "122second");
  menu.add(1, 3, 3, "211third");
  menu.add(1, 4, 4, "311forth");
  SubMenu subMenu = menu.addSubMenu(2, 1, 1, "桃子" );
  subMenu.add(2, 101, 103, "大桃子");
  subMenu.add(2, 102, 102, "小桃子").setOnMenuItemClickListener(new MenuItemClickListener());
  
  return super.onCreateOptionsMenu(menu);
 }

 public class MenuItemClickListener implements MenuItem.OnMenuItemClickListener {

  @Override           
  public boolean onMenuItemClick(MenuItem item) {    
   Toast.makeText(MenuActivity.this, item.getTitle()+ " is selected", Toast.LENGTH_SHORT).show();
   
   return true; //true表示完成当前item的click处理,不再传递到父类处理                }   
  }
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  // TODO Auto-generated method stub
   if (v.getId() == R.id.contextMenuButton1) {
    menu.setHeaderTitle("button1 menu");
    menu.add(300, 300, 300, "context menu1").setOnMenuItemClickListener(new MenuItemClickListener());
    menu.add(300, 301, 301, "context menu2").setOnMenuItemClickListener(new MenuItemClickListener());
    SubMenu subMenu = menu.addSubMenu(2, 1, 1, "桃子" );
    subMenu.add(2, 101, 103, "大桃子").setOnMenuItemClickListener(new MenuItemClickListener());
    subMenu.add(2, 102, 102, "小桃子").setOnMenuItemClickListener(new MenuItemClickListener());
   } else {
    menu.setHeaderTitle("button2 menu");
    menu.add(301, 300, 300, "context menu21").setOnMenuItemClickListener(new MenuItemClickListener());
    menu.add(301, 301, 301, "context menu22").setOnMenuItemClickListener(new MenuItemClickListener());
   }
  super.onCreateContextMenu(menu, v, menuInfo);
 }

 @Override
 public boolean onMenuItemSelected(int featureId, MenuItem item) {
  // TODO Auto-generated method stub
  TextView tv = (TextView) findViewById(R.id.menuTextView2);
  
  switch(item.getItemId()) {
  case 4:
   Toast.makeText(MenuActivity.this, item.getTitle()+ " is selected,item id is 4!", Toast.LENGTH_LONG).show();
   break;
  case 400:
   tv.setText("another UI");
   break;
  case 401:
   tv.setText("Menu UI");
   break;
  default:
   Toast.makeText(MenuActivity.this, item.getTitle()+ " is selected,item id is not 4!!", Toast.LENGTH_LONG).show();
   break;
  }
  return super.onMenuItemSelected(featureId, item);
 }

 @Override   
 public boolean onPrepareOptionsMenu(Menu menu) {  
  // TODO Auto-generated method stub      
  TextView tv = (TextView) findViewById(R.id.menuTextView2);
  String currentText = tv.getText().toString();    
  if("Menu UI".equals(currentText)){       
   menu.clear();//先清掉菜单    
   
   MenuItem item = menu.add(0, 400, 401, "to another UI");
   //可以通过点击这个菜单项来改变tv1的值这样(变成N)就可以测试了      
   item.setIcon(android.R.drawable.alert_dark_frame);//android自带的图标     
   }      
  if("another UI".equals(currentText)){   
   menu.clear();//先清掉菜单       
   MenuItem item = menu.add(0, 401, 402, "to Menu UI");
   //可以通过点击这个菜单项来改变tv1的值这样(变成M)就可以测试了     
   item.setIcon(android.R.drawable.alert_light_frame);    
   }      
  menu.add(0, 402, 403, "Now is " + currentText);
  //现在共有两个菜单子项    
  return super.onPrepareOptionsMenu(menu);
  }
 
}

你可能感兴趣的:(android ui)