android 菜单一打开就是子菜单看了书上的原代码没找到原因

daoyang 发布于 2012/01/11 09:52
阅读 396
收藏 0
package com.android.li.chapter04;  

import android.app.Activity;  
import android.content.res.Resources;  
import android.graphics.drawable.Drawable;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.MenuItem;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.Toast;  

public class OpitionMenu extends Activity {  

    @Override 
    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main); 
   

        Button b = (Button) findViewById(R.id.toggle);  

        OnClickListener l = new OnClickListener() {  

            public void onClick(View v) {  

                toggleMenuGroup();  

            }  

        };  

        b.setOnClickListener(l);  

    }  

   

    public void toggleMenuGroup() {  

        // TODO Auto-generated method stub  

        if (mIsGroupVisible == true) {  

            mMenu.setGroupVisible(1, false);  

            mIsGroupVisible = false;  

            Toast.makeText(this, "隐藏了GroupId为1的菜单群", Toast.LENGTH_SHORT).show();  

        } else {  

            mMenu.setGroupVisible(1, true);  

            mIsGroupVisible = true;  

            Toast.makeText(this, "显示了GroupId为1的菜单群", Toast.LENGTH_SHORT).show();  

   

        }  

    }  

   

    private Menu mMenu = null;  

    private boolean mIsGroupVisible = true;  

   

    @Override 

    public boolean onCreateOptionsMenu(Menu menu) {  

        mMenu = menu;  

        Resources res = getResources();  

        Drawable icon1 = res.getDrawable(R.drawable.ding);  

        Drawable icon2 = res.getDrawable(R.drawable.icong);  

        Drawable icon3 = res.getDrawable(R.drawable.fly);  

        menu.add(0, 1, Menu.NONE, "金").setIcon(icon1);  

        menu.add(1, 2, Menu.NONE, "木").setIcon(icon2);  

        menu.add(0, 3, Menu.NONE, "水").setIcon(icon3);  

        menu.add(1, 4, Menu.NONE, "火");  

        menu.add(0, 5, Menu.NONE, "土");  

        menu.add(1, 6, Menu.NONE, "甲");  

        menu.add(0, 7, Menu.NONE, "乙");  

        menu.add(1, 8, Menu.NONE, "丙").setIcon(icon3);  

        menu.add(0, 9, Menu.NONE, "丁").setIcon(icon1);  

        return true;  

    }  

   

    @Override 

    public boolean onOptionsItemSelected(MenuItem item) {  

        // TODO Auto-generated method stub  

        TextView tv = (TextView) findViewById(R.id.result);  

        int id = item.getItemId();  

        String title = item.getTitle().toString();  

        tv.setText("您选择的id是:" + id + "标题是:" + title);  

        return false;  

    }  

} 
//上面是我写的代码一直没找到原因 
//下面是我从书上看的代码没问题 
package com.haiii.android.learning; 
import android.app.Activity;  

import android.content.res.Resources;  

import android.graphics.drawable.Drawable;  

import android.os.Bundle;  

import android.view.Menu;  

import android.view.MenuItem;  

import android.view.SubMenu;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

import android.widget.TextView;  

import android.widget.Toast; 

public class OptionMenu extends Activity {  

/** Called when the activity is first created. */ 

@Override 

public void onCreate(Bundle savedInstanceState) {  

 super.onCreate(savedInstanceState);  

 setContentView(R.layout.main); 
 Button b = (Button) findViewById(R.id.toggle);  

 OnClickListener l = new OnClickListener() {  

  public void onClick(View v) {  

   toggleMenuGroup();  

  }  

 };  

 b.setOnClickListener(l);  

} 
public void toggleMenuGroup() {  

  if (mIsGroupVisible == true) {  

   mMenu.setGroupVisible(1, false);  

   mIsGroupVisible = false;  

   Toast.makeText(this, "隐藏了GroupId为1的菜单群", Toast.LENGTH_SHORT).show();  

  } else {  

   mMenu.setGroupVisible(1, true);  

   mIsGroupVisible = true;  

   Toast.makeText(this, "显示了GroupId为1的菜单群", Toast.LENGTH_SHORT).show();  

  }  

 } 
private Menu mMenu = null;  

private boolean mIsGroupVisible = true; 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) {  

  mMenu = menu;  

  Resources res = getResources();/*  

           * Drawable icon =  

           * res.getDrawable(R.drawable.haiii);  

           *   

           * menu.add(0, 1, Menu.NONE,  

           * "金").setIcon(icon); menu.add(1, 2,  

           * Menu.NONE, "木"); menu.add(0, 3,  

           * Menu.NONE, "水"); menu.add(1, 4,  

           * Menu.NONE, "火"); menu.add(0, 5,  

           * Menu.NONE, "土"); menu.add(1, 6,  

           * Menu.NONE, "甲"); menu.add(0, 7,  

           * Menu.NONE, "乙"); menu.add(1, 8,  

           * Menu.NONE, "丙"); menu.add(0, 9,  

           * Menu.NONE, "丁").setIcon(icon);  

           */ 

  // SubMenu master = menu.addSubMenu(3, 1, 21, "Master");  

  // master.add("Nigoo");  

  // master.add("Jack");  

  // master.add("Kerr"); 
  Drawable icon1 = res.getDrawable(R.drawable.haiii);  

  Drawable icon2 = res.getDrawable(R.drawable.haiii);  

  Drawable icon3 = res.getDrawable(R.drawable.haiii);  

  menu.add(0, 1, Menu.NONE, "金").setIcon(icon1);  

  menu.add(1, 2, Menu.NONE, "木").setIcon(icon2);  

  menu.add(0, 3, Menu.NONE, "水").setIcon(icon3);  

  menu.add(1, 4, Menu.NONE, "火");  

  menu.add(0, 5, Menu.NONE, "土");  

  menu.add(1, 6, Menu.NONE, "甲");  

  menu.add(0, 7, Menu.NONE, "乙");  

  menu.add(1, 8, Menu.NONE, "丙").setIcon(icon3);  

  menu.add(0, 9, Menu.NONE, "丁").setIcon(icon1);  

  return true;  

 } 
 @Override 
 public boolean onOptionsItemSelected(MenuItem item) {  

  // TODO Auto-generated method stub  

  TextView tv = (TextView) findViewById(R.id.result);  

  int id = item.getItemId();  

  ;  

  String title = item.getTitle().toString();  

  tv.setText("您选择的id是:" + id + ",标题是:" + title);  

  return false;  

 }  

} 
加载中
返回顶部
顶部