Android开发菜单以及子菜单

【Android开发菜单以及子菜单】白日放歌须纵酒,青春作伴好还乡。这篇文章主要讲述Android开发菜单以及子菜单相关的知识,希望能为你提供帮助。

package com.example.androidtest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; 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){ SubMenu file = menu.addSubMenu("文件"); SubMenu edit = menu.addSubMenu("编辑"); file.add(1,1,1,"新建"); file.add(1,2,1,"打开"); file.add(1,3,1,"保存"); file.setHeaderTitle("文件操作"); file.setHeaderIcon(R.drawable.img1); edit.add(2,1,1,"复制"); edit.add(2,2,1,"粘贴"); edit.add(2,3,1,"剪切"); edit.setHeaderTitle("文件操作"); edit.setHeaderIcon(R.drawable.img1); 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); }}

 

    推荐阅读