概述
android 菜单包括:
- contextmenu(上下文菜单,通过长按调出)
- optionmenu(menu菜单)
- submenu(子菜单)
- 带按钮的单选菜单
- 带按钮的多选菜单
此程序演示了android的上下文菜单,menu按钮菜单,子菜单(包括在xml文件中添加,和在程序中动态添加单选,多选子菜单)以及带按钮单选和多选的菜单
package org.ynu.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MenuActivity extends Activity {
private int groupId1 = 19880716;
private int groupId2 = 19880717;
private ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
ArrayList data = new ArrayList();
HashMap map1 = new HashMap();
map1.put("item", "第一个选项");
HashMap map2 = new HashMap();
map2.put("item", "第二个选项");
HashMap map3 = new HashMap();
map3.put("item", "第三个选项");
HashMap map4 = new HashMap();
map4.put("item", "第四个选项");
HashMap map5 = new HashMap();
map5.put("item", "第五个选项");
data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[] { "item" }, new int[] { R.id.tv });
lv.setAdapter(adapter);
//listview每个项长按时的响应上下文菜单
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, "弹出上下文菜单", Toast.LENGTH_SHORT);
return false;
}
});
//注册上下文菜单
registerForContextMenu(lv);
}
//创建菜单按钮的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu, menu);
menu.getItem(0).setIcon(R.drawable.ic_menu_delete);
menu.getItem(1).setIcon(R.drawable.ic_menu_home);
menu.getItem(2).setIcon(R.drawable.ic_menu_zoom);
return super.onCreateOptionsMenu(menu);
}
//当按钮菜单被点击时响应
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.delete:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
return true;
case R.id.main:
Toast.makeText(this, "主页", Toast.LENGTH_SHORT).show();
return true;
case R.id.zoom:
Toast.makeText(this, "放大", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.context_menu, menu);
menu.setHeaderIcon(R.drawable.icon);
SubMenu submenu1 = menu.addSubMenu("动态添加的菜单1");
submenu1.add(this.groupId1, 0, 0, "动态添加子菜单1");
submenu1.add(this.groupId1, 0, 0, "动态添加子菜单2");
submenu1.add(this.groupId1, 0, 0, "动态添加子菜单3");
submenu1.add(this.groupId1, 0, 0, "动态添加子菜单4");
// 设置改组子菜单为单选,第三个参数为true时,子菜单单选
submenu1.setGroupCheckable(this.groupId1, true, true);
SubMenu submenu2 = menu.addSubMenu("动态添加的菜单2");
submenu2.add(this.groupId2, 0, 0, "动态添加子菜单1");
submenu2.add(this.groupId2, 0, 0, "动态添加子菜单2");
submenu2.add(this.groupId2, 0, 0, "动态添加子菜单3");
submenu2.add(this.groupId2, 0, 0, "动态添加子菜单4");
// 设置改组子菜单为多选第三个参数为false时,子菜单多选
submenu2.setGroupCheckable(this.groupId2, true, false);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.cmenu1:
Toast.makeText(this, "上下文菜单1", Toast.LENGTH_SHORT).show();
return true;
case R.id.cmenu2:
Toast.makeText(this, "上下文菜单2", Toast.LENGTH_SHORT).show();
return true;
case R.id.csubmenu1:
Toast.makeText(this, "来自xml,我还有子菜单", Toast.LENGTH_SHORT).show();
return true;
case R.id.csubmenu2:
Toast.makeText(this, "来自xml,我还有单选子菜单", Toast.LENGTH_SHORT).show();
return true;
case R.id.csubmenu3:
Toast.makeText(this, "来自xml,我还有多选子菜单", Toast.LENGTH_SHORT).show();
return true;
case R.id.buttonmenu1:
buildRadioDialog();
return true;
case R.id.buttonmenu2:
builderCheckBoxDialog();
return true;
default:
//当按钮被选中时,设置其选中状态为true,这样才能看到在页面上有选中的radio
if (item.getGroupId() == this.groupId1) {
Toast.makeText(this, "动态添加的菜单1的子菜单", Toast.LENGTH_SHORT).show();
if (!item.isChecked()) {
item.setChecked(true);
Toast.makeText(this, "您选的是" + item.getTitle(),
Toast.LENGTH_SHORT).show();
}
}
if (item.getGroupId() == this.groupId2) {
Toast.makeText(this, "动态添加的菜单2的子菜单", Toast.LENGTH_SHORT).show();
if (!item.isChecked()) {
item.setChecked(true);
Toast.makeText(this, "您选的是" + item.getTitle(),
Toast.LENGTH_SHORT).show();
}
}
if (!item.isChecked()) {
item.setChecked(true);
Toast.makeText(this, "您选的是" + item.getTitle(),
Toast.LENGTH_SHORT).show();
}
return super.onContextItemSelected(item);
}
}
//上下文菜单关闭时
@Override
public void onContextMenuClosed(Menu menu) {
// TODO Auto-generated method stub
Toast.makeText(this, "上下文菜单被关", Toast.LENGTH_SHORT).show();
super.onContextMenuClosed(menu);
}
private void buildRadioDialog() {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选按钮菜单");
final String[] titles = new String[] { "白色", "橙色", "蓝色" };
//-1表示默认没有按钮被选中
builder.setSingleChoiceItems(titles, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, titles[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, "取消", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
private void builderCheckBoxDialog() {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选按钮");
final String[] titles = new String[] { "白色", "橙色", "蓝色" };
builder.setMultiChoiceItems(titles,
new boolean[] { false, false, false },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
//当checkbox选中时响应
if (isChecked) {
Toast.makeText(MenuActivity.this,
titles[which] + "被选中", Toast.LENGTH_SHORT)
.show();
}
//当checkbox未选中时响应
if (!isChecked) {
Toast.makeText(MenuActivity.this,
titles[which] + "被取消", Toast.LENGTH_SHORT)
.show();
}
}
});
//确定按钮事件响应
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
//取消按钮 事件响应
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MenuActivity.this, "取消", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
}
contex_menu.xml文件:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/cmenu1" android:title="上下文菜单1" /> <item android:id="@+id/cmenu2" android:title="上下文菜单2" /> <item android:id="@+id/csubmenu1" android:title="来自xml,我还有子菜单"> <menu> <item android:title="子菜单1"></item> <item android:title="子菜单2"></item> <item android:title="子菜单3"></item> </menu> </item> <item android:id="@+id/csubmenu2" android:title="来自xml,我还有单选子菜单"> <menu> <group android:checkableBehavior="single"> <item android:title="子菜单1"></item> <item android:title="子菜单2"></item> <item android:title="子菜单3"></item> </group> </menu> </item> <item android:id="@+id/csubmenu3" android:title="来自xml,我还有多选子菜单"> <menu> <group android:checkableBehavior="all"> <item android:title="子菜单1"></item> <item android:title="子菜单2"></item> <item android:title="子菜单3"></item> </group> </menu> </item> <item android:id="@+id/buttonmenu1" android:title="带按钮的单选子菜单"> </item> <item android:id="@+id/buttonmenu2" android:title="带按钮的多选子菜单"> </item> </menu>
menu.xml文件:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/delete" android:title="删除" /> <item android:id="@+id/main" android:title="主页"> <menu> <item android:id="@+id/create_new" android:title="子菜单1" /> <item android:id="@+id/open" android:title="子菜单2" /> </menu> </item> <item android:id="@+id/zoom" android:title="放大" /> </menu>
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
item.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dip" android:textColor="#065fb9" > </TextView> </RelativeLayout>
效果图
最后
以上就是痴情大米为你收集整理的android 菜单小结--android menu 小结的全部内容,希望文章能够帮你解决android 菜单小结--android menu 小结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复