选项菜单
1.菜单的生命周期
* onCreateOptionsMenu() 选项菜单创建时调用(启动Activity时调用 只调用1次)
* onPrepareOptionsMenu() 选项菜单准备时调用(点击Menu键时调用 调用多次)
* onOptionsItemSelected() 选项菜单项选中时调用
* onOptionsMenuClosed() 选项菜单关闭时
2.onCreateOptionsMenu()方法
在onCreateOptionsMenu()中添加菜单项,建议使用
menu.add(1, Menu.FIRST, Menu.FIRST, "xx");
menu.add(1, Menu.FIRST+1, Menu.FIRST+1, "xx");
menu.add(1, Menu.FIRST+2, Menu.FIRST+2, "xx");
添加子菜单
SubMenu sub = menu.addSubMenu(1, Menu.FIRST+4, Menu.FIRST+4, "xxx");
在子菜单中添加菜单项
sub.add(2, Menu.FIRST+5, Menu.FIRST+5, "xx");
sub.add(2, Menu.FIRST+6, Menu.FIRST+6, "xx");
sub.add(2, Menu.FIRST+7, Menu.FIRST+7, "xx");
3.onOptionsItemSelected()方法
在onOptionsItemSelected()中进行菜单项选中事件的操作 比如
int id = item.getItemId();
switch(id) {
case Menu.FIRST:
...
break;
case Menu.FIRST+1:
...
break;
}
例:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, Menu.FIRST, Menu.FIRST, "红");
menu.add(1, Menu.FIRST + 1, Menu.FIRST + 1, "黄");// 菜单项
menu.add(1, Menu.FIRST + 2, Menu.FIRST + 2, "蓝"); // 菜单项
menu.add(1, Menu.FIRST + 3, Menu.FIRST + 3, "绿"); // 菜单项
// 添加子菜单
SubMenu sub = menu.addSubMenu(1, Menu.FIRST + 4, Menu.FIRST + 4, "更多"); // 子菜单
sub.add(2, Menu.FIRST + 5, Menu.FIRST + 5, "其他1");
sub.add(2, Menu.FIRST + 6, Menu.FIRST + 6, "其他2");
sub.add(2, Menu.FIRST + 7, Menu.FIRST + 7, "其他3");
// Inflate the menu; this adds items to the action bar if it is present.
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case Menu.FIRST:
textview.setTextColor(Color.RED);
break;
case Menu.FIRST + 1:
textview.setTextColor(Color.YELLOW);
break;
case Menu.FIRST + 2:
textview.setTextColor(Color.BLUE);
break;
case Menu.FIRST + 3:
textview.setTextColor(Color.GREEN);
break;
case Menu.FIRST + 5:
textview.setTextColor(Color.CYAN);
break;
case Menu.FIRST + 6:
textview.setTextColor(Color.GRAY);
break;
case Menu.FIRST + 7:
textview.setTextColor(Color.MAGENTA);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
}
上下文菜单
第一种方式
1.生命周期
onCreateContextMenu();//创建菜单时调用
onContextItemSelected();//选中上下文时调用
onContextMenuClosed();//关闭菜单时调用
例:
registerForContextMenu(textview);
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.textview:
menu.add(3, Menu.FIRST+5, Menu.FIRST+5, "红");
menu.add(3, Menu.FIRST+6, Menu.FIRST+6, "黄");
SubMenu submenu = menu.addSubMenu(3, Menu.FIRST+7, Menu.FIRST+7, "其他");
submenu.add(4, Menu.FIRST+8, Menu.FIRST+8, "蓝");
submenu.add(4, Menu.FIRST+9, Menu.FIRST+9, "绿");
break;
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case Menu.FIRST+5:
textview.setBackgroundColor(Color.RED);
break;
case Menu.FIRST+6:
textview.setBackgroundColor(Color.YELLOW);
break;
case Menu.FIRST+8:
textview.setBackgroundColor(Color.BLUE);
break;
case Menu.FIRST+9:
textview.setBackgroundColor(Color.GREEN);
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onContextMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onContextMenuClosed(menu);
}
第二种方式
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/m1"
android:title="m1">
<menu>
<item
android:id="@+id/m5"
android:title="m5"></item>
</menu>
</item>
<item
android:id="@+id/m2"
android:title="m2"></item>
<item
android:id="@+id/m3"
android:title="3"></item>
<item
android:id="@+id/m4"
android:title="m4"></item>
</menu>
public class MainActivity extends AppCompatActivity {
private ListView listview;
private List<String> list;
private ArrayAdapter<String> adapter;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
initData();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
registerForContextMenu(listview);
}
private void initData() {
list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("王者--"+i);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
if(menuInfo!=null){
position = menuInfo.position;
}
switch (item.getItemId()){
case 1:
list.add(0,"王者");
break;
case 2:
list.set(position,"王者归来");
adapter.notifyDataSetChanged();
break;
case 3:
list.remove(position);
adapter.notifyDataSetChanged();
break;
}
return super.onContextItemSelected(item);
}
}