Android中menu详解(转)

   Android中菜单的使用时非常频繁的,能够达到很好的使用效果,其实他的实现方法非常简单,下面为大家讲解,首先看图:

 JAVA代码:

package com.smart;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class SmartMenu extends Activity {
public static final int ITEM0=Menu.FIRST;//系统值
public static final int ITEM1=Menu.FIRST+1;
public static final int ITEM2=Menu.FIRST+2;
public static final int ITEM3=Menu.FIRST+3;
Button button1;
Button button2;
Button button3;
Button button4;
// private Menu menu;
// private Menu menu

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
        button4=(Button)findViewById(R.id.button4);
        button1.setVisibility(View.INVISIBLE);
        button2.setVisibility(View.INVISIBLE);
        button3.setVisibility(View.INVISIBLE);
        button4.setVisibility(View.INVISIBLE);
//       
    }
   
   
    @Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, ITEM0, 0,"显示button1");
  menu.add(0, ITEM1,0,"显示button2");
  menu.add(0, ITEM2,0,"新建");
  menu.add(0, ITEM3,0,"打开");
  menu.findItem(ITEM1);//可以设置按钮的背景
  return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 
  switch (item.getItemId()) {
  case ITEM0:
   actionClickMenuItem1();
   break;
  case ITEM1:
   actionClickMenuItem2();
   break;
  case ITEM2:
   actionClickMenuItem3();
   break;
  case ITEM3:
   actionClickMenuItem4();
   break;
  }
 
  return super.onOptionsItemSelected(item);
}

//第一个事件的方法

   private void actionClickMenuItem1(){
    setTitle("button1可见");
    button1.setVisibility(View.VISIBLE);//可见
    button2.setVisibility(View.INVISIBLE);//不可见
   
   }
   //第2个事件的方法
   private void actionClickMenuItem2(){
    setTitle("button2可见");
    button1.setVisibility(View.INVISIBLE);//不可见
    button2.setVisibility(View.VISIBLE);//可见
   
   }
   private void actionClickMenuItem3(){
    setTitle("button2可见");
    button1.setVisibility(View.INVISIBLE);//不可见
    button2.setVisibility(View.INVISIBLE);//不可见
    button3.setVisibility(View.VISIBLE);//可见
    button4.setVisibility(View.INVISIBLE);//不可见
  
   
   
  }
  
   private void actionClickMenuItem4(){
    setTitle("button2可见");
    button1.setVisibility(View.INVISIBLE);//不可见
    button2.setVisibility(View.INVISIBLE);//不可见
    button3.setVisibility(View.INVISIBLE);//不可见
    button4.setVisibility(View.VISIBLE);//可见
   
   }
   
}

 

XML布局文件:

<?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="wrap_content"
    >
   
    <Button
    android:id="@+id/button1"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="我的菜单我做主_1"
    />
   
      <Button
    android:id="@+id/button2"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="我的菜单我做主_2"
    />
   
     <Button
    android:id="@+id/button3"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="小子,给记住这代码"
    />
   
      <Button
    android:id="@+id/button4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="哈哈,你欺负我呀,我就能记住"
    />
    
</LinearLayout>

posted @ 2011-09-18 07:55  郑文亮  阅读(573)  评论(0编辑  收藏  举报