package com.example.optionsmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
MenuItem menuItem=menu.add(1,100,1,"菜单一");
menuItem.setTitle("aaa");
menuItem.setIcon(R.drawable.ic_launcher);//api>=11的时候不显示图标
menu.add(1,101,1,"菜单二");
menu.add(1,102,1,"菜单三");
menu.add(1,103,1,"菜单四");
menu.add(1,104,1,"菜单五");
menu.add(1,105,1,"菜单六");
menu.add(1,106,1,"菜单七");
return true;
}
//重写onOptionsItemSelected方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 100:
//Toast.makeText(MainActivity.this,"点击了菜单一",Toast.LENGTH_LONG).show();
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
item.setIntent(intent);
break;
case 101:
Toast.makeText(MainActivity.this,"点击了菜单二",Toast.LENGTH_LONG).show();
break;
case 102:
Toast.makeText(MainActivity.this,"点击了菜单三",Toast.LENGTH_LONG).show();
break;
case 103:
Toast.makeText(MainActivity.this,"点击了菜单四",Toast.LENGTH_LONG).show();
break;
case 104:
Toast.makeText(MainActivity.this,"点击了菜单五",Toast.LENGTH_LONG).show();
break;
case 105:
Toast.makeText(MainActivity.this,"点击了菜单六",Toast.LENGTH_LONG).show();
break;
case 106:
Toast.makeText(MainActivity.this,"点击了菜单七",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
// 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();
// if (id == R.id.item_menu1) {
// return true;
// }
// return super.onOptionsItemSelected(item);
// }
}
动态添加菜单栏Pluse
最新推荐文章于 2022-07-17 23:20:54 发布