Menu的用法

一、创建选项菜单的步骤:
1、复写Activity的onCreateOptionsMenu(), 添加菜单选项
2、复写Activity的onOptionsItemSelected方法, 实现菜单item的点击事件
3、添加选项菜单item有两种方法:
1)静态填充menu文件, getMenuInflater()
2) 动态添加item, menu.add()
此处介绍两种常用菜单:ContextMenu和PopupMenu
二、上下文菜单:长按的时候会弹出另外一个菜单
使用ContextMenu的步骤:
1 复写onCreateContextMenu创建一个上下文菜单
2 复写onContextItemSelected监听item点击事件
3 registerForContextMenu将上下文菜单注册到相应的View上

package org.mobile.train.day11_qf_contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 使用ContextMenu的步骤:
 * 1 复写onCreateContextMenu创建一个上下文菜单
 * 2 复写onContextItemSelected监听item点击事件
 * 3 registerForContextMenu将上下文菜单注册到相应的View上
 */
public class MainActivity extends Activity {

    private static final int ITEM1 = Menu.FIRST;
    private static final int ITEM2 = ITEM1 + 1;
    private static final int ITEM3 = ITEM2 + 1;

    private ListView listView;
    private String[] characters;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //R.array.characters存的是人物的名字
        characters = getResources().getStringArray(R.array.characters);
        ArrayAdapter adapter = new ArrayAdapter<>(this, 
                android.R.layout.simple_list_item_1, characters);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        //给ListView注册上下文菜单
        registerForContextMenu(listView);
    }

    /**
     * 创建上下文菜单的方法
     */
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //在title中添加上被选中的人物名字
        AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;

        String title = characters[acmi.position];
        menu.setHeaderTitle("人物简介:" + title);
        menu.add(0, ITEM1, 0, "特长");
        menu.add(0, ITEM2, 0, "战斗力");
        menu.add(0, ITEM3, 0, "经典语录");
    }

    /**
     * 上下文菜单item的点击事件
     */
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case ITEM1:
            Toast.makeText(this, "特长 clicked", 0).show();
            break;
        case ITEM2:
            Toast.makeText(this, "战斗力 clicked", 0).show();
            break;
        case ITEM3:
            Toast.makeText(this, "经典语录 clicked", 0).show();
            break;
        }
        return super.onContextItemSelected(item);
    }
}

三、PopupMenu:点击控件时,在控件边上显示一个菜单,点击该菜单会在控件某个位置显示选中菜单的值。
1 new PopupMenu(Context context, View anchor);//anchor锚,创建PopupMenu时要指定依赖的View。
2 PopupMenu.getMenuInflater.inflate(menuId, popupMenu.getMenu);
3 PopupMenu.setOnMenuItemClickListener,
此类中重写onMenuItemClick()时,把getXxx值设置给所依赖View的合适位置,例如setTitle,setText等
4 PopupMenu.show();
【注意:TextView在version4.0之前默认clickable默认是false。需要手动设置为clickable=”true”。】

你可能感兴趣的:(Android,menu,Context,popup)