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

Android Menu

时间:2018-10-04 17:22:41      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:cti   ...   get   click   bre   encoding   上下文   cli   popup   

1.ContextMenu

上下文菜单,就是长按某个控件后弹出菜单

1.1选项菜单Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="none">
        <item android:id="@+id/item_context_item1" android:title="Item1"/>
        <item android:id="@+id/item_context_item2" android:title="Item2"/>
        <item android:id="@+id/item_context_item3" android:title="Item3"/>
    </group>
</menu>

1.2 为View注册上下文菜单

registerForContextMenu(view);

1.3创建Menu

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        MenuInflater inflator=new MenuInflater(this);
        inflator.inflate(R.menu.menu_context,menu);

        super.onCreateContextMenu(menu, v, menuInfo);
    }

1.4 Item 点击事件

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.item_context_item1:
                toast("Item1");
                break;
             ....
        }
        return super.onContextItemSelected(item);
    }

2.PopupMenu

// Show Popup Menu
void showPopupMenu(){

    // 为View指定 PopupMenu
    PopupMenu pm=new PopupMenu(MenuActivity.this,btnPopupMenu);
    // 指定 Menu 文件
    pm.getMenuInflater().inflate(R.menu.menu_test,pm.getMenu());
    //Item 点击事件
    pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()){
                case R.id.menu_item1:
                    toast("Menu1");
                    break;
                case R.id.menu_item2:
                    toast("Menu2");
                    break;
            }
            return true;
        }
    });
    pm.show(); //显示
}

Android Menu

标签:cti   ...   get   click   bre   encoding   上下文   cli   popup   

原文地址:https://www.cnblogs.com/-Tiger/p/9742475.html

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