上篇学习了 安卓的 notification 通知实现
(1)OptionMenu 的 动态和 静态实现
(2)Submenu 的动态和静态实现
(3)ContextMenu 的 动态和静态实现
/**
* 重写 onCreateOptionsMenu
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//动态实现
MenuItem item= menu.add(1, 1, 1,"河南");
item.setTitle("原");
menu.add(1, 2, 1,"理工");
menu.add(1, 3, 1,"大学");
menu.add(2, 3, 1,"焦作");
//加载 布局实现
getMenuInflater().inflate(R.menu.option_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* 菜单 点击 重写onMenuItemSelected
*/
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Toast.makeText(getApplicationContext(),featureId+" | "+item.getTitle(),Toast.LENGTH_LONG).show();
// item 点击
switch (item.getItemId()) {
case R.id.option_item1:
Intent intent= new Intent(MainActivity.this,OptionActivity.class);
intent.putExtra("data",item.getTitle());
item.setIntent(intent);
break;
case R.id.option_item2:
Intent intent1= new Intent(MainActivity.this,ContextmenuActivity.class);
item.setIntent(intent1);
break;
}
return super.onMenuItemSelected(featureId, item);
}
/**
* 创建菜单
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//动态创建(代码创建)
//添加第一项
// SubMenu subMenu= menu.addSubMenu("工程");
// subMenu.add(1, 1, 1,"打开工程");
// subMenu.add(1, 2, 1,"关闭工程");
// subMenu.add(1, 3, 1,"Build工程");
// subMenu.add(1, 4, 1,"Clean工程");
// //添加第二项
// SubMenu subMenu1= menu.addSubMenu("窗口");
// subMenu1.add(2, 1, 1,"new Window");
// subMenu1.add(2, 2, 1,"Editer");
// subMenu1.add(2, 3, 1,"Show View");
// subMenu1.add(2, 4, 1,"Preference");
//布局实现
getMenuInflater().inflate(R.menu.sub_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* item 点击事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//可以同过 id 判断
Toast.makeText(getApplicationContext(),item.getTitle(),Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
/**
* 重新 onCreateContextMenu 添加菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// 重写 ContextMenu 方法
// 动态加载
menu.add(1, 1, 1, "干嘛");
menu.add(1, 2, 1, "哇哦");
menu.add(1, 3, 1, "咳咳");
menu.add(1, 4, 1, "哈哈");
//布局添加
getMenuInflater().inflate(R.menu.context_menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
/**
* 菜单 项点击事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
textView1.setText(item.getItemId()+" | "+item.getTitle());
//判断 点击的id , 不同的操作使用 switch
switch (item.getItemId()) {
case 1:
Toast.makeText(getApplicationContext(),"我就是要"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(),"你快看"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(getApplicationContext(),"老头子!"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(getApplicationContext(),"你是。。。!"+item.getTitle(),Toast.LENGTH_SHORT).show();
break;
case R.id.context_item1:
Toast.makeText(getApplicationContext(),"context_item1",Toast.LENGTH_SHORT).show();
break;
case R.id.context_item2:
Toast.makeText(getApplicationContext(),"context_item2",Toast.LENGTH_SHORT).show();
break;
case R.id.context_item3:
Toast.makeText(getApplicationContext(),"context_item3",Toast.LENGTH_SHORT).show();
break;
case R.id.context_item4:
Toast.makeText(getApplicationContext(),"context_item4",Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
http://download.csdn.net/detail/lablenet/9050689