码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发菜单以及子菜单

时间:2017-04-07 09:28:07      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:set   get   div   idg   submenu   ase   view   span   activity   

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);
    }

}

 

Android开发菜单以及子菜单

标签:set   get   div   idg   submenu   ase   view   span   activity   

原文地址:http://www.cnblogs.com/guojinyu/p/6676458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!