大家都知道,任务程序离不开菜单,菜单无处不在,哪我们一定要学好ANDROID中的菜单,它给我带来了不少的功能。相信大家都知道的。好,现在我们开始吧,下面的代码很简单。看图:
- 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 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>
本文转自 llb988 51CTO博客,原文链接:http://blog.51cto.com/llb988/525525,如需转载请自行联系原作者