Android按键之Menu介绍

Android按键之Menu介绍

Android手机一般都有三个键,返回键、Home键、菜单键;

Android系统的菜单支持主要通过4个接口来实现。

Android按键之Menu介绍Android按键之Menu介绍

从上图可以看出Menu是一个父类接口,它下面有两个子类一个是ContextMenu:它表示是一个上下文菜单,SubMenu表示一个子菜单。它们都可以包含一个或多个的MenuItem菜单项

Menu接口定义了两个方法来添加菜单项和添加子菜单。一个通过add()方法添加菜单项,addSubMenu()方法来添加子菜单。而SubMenu与ContextMenu继承了Munu接口的方法之外,还提供了一些的方法。比如设置菜单的标题、图标等;

Android按键之Menu介绍Android按键之Menu介绍Android按键之Menu介绍

开发菜单和子菜单有两种方式;一是在Activity重写的方法onCreateOptionsMenu(Menu menu)方法中调用menu对象添加,然后在重写Activity的onOptionsItemSelected来响应选择的菜单项。二是通过资源文件来定义菜单项,然后在onCreateOptionsMenu方法中加载这个xml资源。onCreateOptionsMenu方法在单击Menu键时触发。

而开发上下文菜单与上类似:分为三个步骤:一是重写onCreateContextMenu(ContextMenu menu,View source,ContextMenu.MenuInfo info)方法,然后调用registerForContextMenu(View viwe)方法为控件注册上下文菜单,最后重写onContextItemSelected(Menuitem item)来响应选择的菜单项

实例如下:

    欢迎加入QQ交流3群:317874559

1、代码中添加:

package com.android.xiong.memu;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
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;

public class MainActivity extends Activity {

// 定义菜单项的ItemId
private final int NEWS = 00;
private final int COLOR = 01;
private final int FONT = 02;
private final int RED = 001;
private final int BULE = 002;
private final int GREED = 003;

private final int BIG = 004;
private final int SMALL = 005;

Builder bu;

Button show;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu = new AlertDialog.Builder(this);
show = (Button) findViewById(R.id.context);
// 为Button注册上下文菜单
registerForContextMenu(show);

}

// 用户单击Menu键时触发
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 添加新建菜单项
menu.add(0, NEWS, 0, "新建");
// 添加颜色子菜单
SubMenu color = menu.addSubMenu(0, COLOR, 0, "颜色");
color.setHeaderTitle("选择颜色");
color.add(0, RED, 0, "红色");
color.add(0, BULE, 0, "蓝色");
color.add(0, GREED, 0, "绿色");
// 添加字体的菜单
SubMenu font = menu.addSubMenu(0, FONT, 0, "字体");
font.setHeaderTitle("选择字体");
font.add(0, BIG, 0, "大字体");
font.add(0, SMALL, 0, "小字体");
return super.onCreateOptionsMenu(menu);
}

// 当用户单击菜单项触发
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case RED:
bu.setMessage("红色").setPositiveButton("确定", null).show();
break;
case BULE:
bu.setMessage("蓝色").setPositiveButton("确定", null).show();
break;
case GREED:
bu.setMessage("绿色").setPositiveButton("确定", null).show();
break;
case BIG:
bu.setMessage("大号").setPositiveButton("确定", null).show();
break;
case SMALL:
bu.setMessage("小号").setPositiveButton("确定", null).show();
break;
case NEWS:
bu.setMessage("新建").setPositiveButton("确定", null).show();
break;
}
return true;
}

// 长按组件响应的上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, RED, 0, "红色");
menu.add(0, BULE, 0, "蓝色");
menu.add(0, GREED, 0, "绿色");
}

// 点击上下文菜单项触发
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case RED:
bu.setMessage("红色").setPositiveButton("确定", null).show();
break;
case BULE:
bu.setMessage("蓝色").setPositiveButton("确定", null).show();
break;
case GREED:
bu.setMessage("绿色").setPositiveButton("确定", null).show();
break;

}
return true;
}

}

2、XML资源定义菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <!-- 添加菜单项 -->
  <item 
    android:id="@+id/menu_news"
    android:title="@string/news"/>
  <!-- 添加子菜单 -->
  <item 
    android:title="@string/color" >
    <menu>
      <!-- 单项选择 -->
      <group android:checkableBehavior="single">
        <!-- 选中红色 -->
        <item android:id="@+id/color_red"
          android:checked="true"
          android:title="@string/red"/>
        <item android:id="@+id/color_bule"
          android:title="@string/bule"/>
        <item android:id="@+id/color_greed"
          android:title="@string/greed"/>
        
      </group>
    </menu>
    </item>
  <item 
    android:title="@string/font" >
    <menu>
      <!-- 多项选择 -->
      <group android:checkableBehavior="all">
        <item android:id="@+id/font_big"
          android:title="@string/big"/>
        <item android:id="@+id/font_Small"
          android:title="@string/small"/>
        
      </group>
    </menu>
    </item>

</menu>

package com.android.xiong.menu2;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Button show;
Builder bu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu = new AlertDialog.Builder(this);
show=(Button)findViewById(R.id.bt_show);
registerForContextMenu(show);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.color_red:
bu.setMessage("红色").setPositiveButton("确定", null).show();
break;
case R.id.color_bule:
bu.setMessage("蓝色").setPositiveButton("确定", null).show();
break;
case R.id.color_greed:
bu.setMessage("绿色").setPositiveButton("确定", null).show();
break;
case R.id.font_big:
bu.setMessage("大号").setPositiveButton("确定", null).show();
break;
case R.id.font_Small:
bu.setMessage("小号").setPositiveButton("确定", null).show();
break;
case R.id.menu_news:
bu.setMessage("新建").setPositiveButton("确定", null).show();
break;
}
return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.main, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.color_red:
bu.setMessage("红色").setPositiveButton("确定", null).show();
break;
case R.id.color_bule:
bu.setMessage("蓝色").setPositiveButton("确定", null).show();
break;
case R.id.color_greed:
bu.setMessage("绿色").setPositiveButton("确定", null).show();
break;
case R.id.font_big:
bu.setMessage("大号").setPositiveButton("确定", null).show();
break;
case R.id.font_Small:
bu.setMessage("小号").setPositiveButton("确定", null).show();
break;
case R.id.menu_news:
bu.setMessage("新建").setPositiveButton("确定", null).show();
break;
}
return true;
}


}

PopMenu表示一个弹出式菜单:使用的的步骤1、创建PopupMenu对象;2、通过MenuInflater的inflate()方法将菜单填充到PopupMenu中3、调用PopupMenu的show()方法显示弹出菜单

实例:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  
  <group
    android:checkableBehavior="single">
    <item android:id="@+id/red" android:title="@string/red"/>
    <item android:id="@+id/bule" android:title="@string/bule"/>
    <item android:id="@+id/greed" android:title="@string/greed"/>
  </group>

</menu>


package com.android.xiong.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

Button show;
PopupMenu pop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show=(Button)findViewById(R.id.show);
setPopupMenu(show);
show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
pop.show();

}
});

}

public void setPopupMenu(View v){
//创建PopupMenu对象
pop=new PopupMenu(this, v);
//将菜单资源加载到Pop中
getMenuInflater().inflate(R.menu.main,pop.getMenu());
pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {

@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
break;
case R.id.bule:
Toast.makeText(MainActivity.this, "蓝色", Toast.LENGTH_SHORT).show();
break;
case R.id.greed:
Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}