MainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.txt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//方法一、静态添加 xml方式添加菜单
// getMenuInflater().inflate(R.menu.menu_main, menu);
//方法二、动态添加 代码添加菜单项
MenuItem item = menu.add(1, 100, 1, "赵1"); //参数(groupID , ItemID , order ,title)
item.setTitle("aaa");
menu.add(1,101,1,"赵2");
menu.add(1,102,1,"赵3");
menu.add(1,5,1,"赵4");
return true;
}
@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();
//noinspection SimplifiableIfStatement
switch (id){
case 100:{
Intent intent = new Intent(MainActivity.this,second_demo.class);
item.setIntent(intent); //跳转到其他activity
textView.setText("点击了" + id);
break;
}
case 101:{
textView.setText("点击了" + id);
break;
}
case 102:{
textView.setText("点击了" + id);
break;
}
case 5:{
textView.setText("点击了" + id);
break;
}
}
return super.onOptionsItemSelected(item);
}
}
---------------------------------------------------------------------------------------------我是华丽丽de分界线-------------------------------------------------------------------------------
SubMenu多级菜单 子菜单
方法一:动态加载
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//添加子菜单
//addSubMenu有子菜单的主菜单
SubMenu file = menu.addSubMenu("文件");
SubMenu edit = menu.addSubMenu("编辑");
//嵌套菜单SubMenu
SubMenu A = menu.addSubMenu("第一层");
SubMenu AA = A.addSubMenu("第二层");
SubMenu AAA = AA.addSubMenu("第三层");
AAA.add(3,1,1,"第四层");
file.add(1,1,1,"新建");
file.add(1,2,1,"打开");
file.add(1,3,1,"保存");
file.setHeaderTitle("文件de操作");
edit.add(2, 1, 1, "复制");
edit.add(2,2,1,"粘贴");
edit.add(2,3,1,"剪切");
edit.setHeaderTitle("编辑de操作");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getGroupId() == 1){
switch (item.getItemId()){
case 1:
Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
break;
}
}else if(item.getGroupId() == 2){
switch (item.getItemId()){
case 1:
Toast.makeText(MainActivity.this,"点击了复制",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
方法二、静态加载
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.edit_copy:
Toast.makeText(MainActivity.this,"点击了copy",Toast.LENGTH_SHORT).show();
break;
case R.id.edit_jianqie:
Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
break;
case R.id.edit_nietian:
Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case R.id.file_new:
Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
break;
case R.id.file_open:
Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
break;
case R.id.file_save:
Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml