android Menu详解

 只要有下面两部分就可以初步的实现一个菜单的操作了噢

public boolean onCreateOptionsMenu(Menu menu){
     MenuInflater inflater = this.getMenuInflater();
     inflater.inflate(R.menu.sub_menu, menu); 

 
  return true;
 }

onCreateOptionsMenu只在选项菜单第一次显示时调用,且只能调用一次

下面是一个菜单及子菜单的示例代码,要仔细阅读哟,看过之后让自己恍然大悟的东西

package cn.class3g.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class AddressListActivity extends Activity {
  
  private static final int SUBMENU = 10;
  private static final int SUBITEM1 = SUBMENU+1;
  private static final int SUBITEM2 = SUBMENU+2;
  private static final int SUBITEM3 = SUBMENU+3;
  private static final int SAVE_AS = SUBMENU+4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.address_list, menu);
  
  SubMenu subMenu =   menu.addSubMenu(1,SUBMENU,1,"通讯录");
    subMenu.add(20,SUBITEM1,1,"大哥");
    subMenu.add(20,SUBITEM2,2,"二哥");
    subMenu.add(20,SUBITEM3,3,"三哥");
      subMenu.setGroupCheckable(20,true,false);
                                    //可选,true,false单选或多选
  
  
      /*  MenuItem item = subMenu.getItem(1);
      
          item.setCheckable(true);
          item.setChecked(true);//处于选种状态
          //设置单个选择
*/ 
       //subMenu.findItem(SUBITEM3).setCheckable(true);设置单个选择
  注意:findItem与getItem的区别
  return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getItemId()){
  case R.id.newFile:
   Toast.makeText(this,"new file",Toast.LENGTH_LONG).show();
   break;
  case R.id.internet:
   
   break;
     
  case R.id.exit:
  this.finish();
   break;

case DELETE:
   menu.removeGroup(1);
   menu.removeItem(SAVE_AS);
   break;
  }
  return super.onOptionsItemSelected(item);
 }
   
   
}

下面是利用for循环实现每一项的监听注册:

package cn.class3g.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;

public class MenuTestActivity1 extends Activity implements OnMenuItemClickListener{
    /** Called when the activity is first created. */
 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
   public boolean onCreateOptionsMenu(Menu menu){
  
    MenuInflater inflater = this.getMenuInflater();
    inflater.inflate(R.menu.optionsmenu,menu);
   
    for(int i=0;i<menu.size();i++){
     menu.getItem(i).setOnMenuItemClickListener(this);
    }
    //实现每一项的监听注册
    return true;
   }

@Override
public boolean onMenuItemClick(MenuItem item) {
 switch(item.getItemId()){
 case R.id.newFile:
  Toast.makeText(this,"new file",Toast.LENGTH_LONG).show();
  break;
 case R.id.exit:
  this.finish();
  break;
 }
 return false;
}
   
}

 

下面是上下文菜单的调用方法

package cn.class3g.activity;

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

public class MenuTestActivity4 extends Activity {
   public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        
         TextView tv= (TextView)this.findViewById(R.id.textView);
         this.registerForContextMenu(tv);
     }
 
   @Override
 public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()){
   case R.id.subitem1:
    Toast.makeText(this,"subitem1",Toast.LENGTH_LONG).show();
    break;
   case R.id.subitem2:
    //this.finish();
    break;
    }
   return super.onOptionsItemSelected(item);
  
 }

 int i=1;
     //每次上下文菜单显示都会调用
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  MenuInflater inflater = this.getMenuInflater();
    inflater.inflate(R.menu.sub_menu,menu);
    
    i++;
    menu.getItem(0).setTitle("x"+i);
    
  super.onCreateContextMenu(menu, v, menuInfo);
 }
  
  
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值