Menu简单应用

在onCreateOptionsMenu(Menu menu)方法里添加菜单项

 menu.add(Menu.NONE, Menu.FIRST + 1, 1, "1").setIcon( android.R.drawable.ic_input_add);

参数分别是分组,id,显示的位置,显示的名称           然后设置图标。

设置点击事件

public boolean onOptionsItemSelected(MenuItem item) {
根据id来进行判断 

 switch (item.getItemId()) {
  case Menu.FIRST + 1:
   Toast.makeText(this, "1", 1000).show();
   break;

}

关闭菜单时调用

public void onOptionsMenuClosed(Menu menu) {
  Toast.makeText(this, "close", 1000).show();
 }

还有一种方法是在menu文件下写xml文件

 

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"
        android:title="添加"
        android:icon="@android:drawable/ic_menu_add"/>
    <item
        android:id="@+id/item2"
        android:title="保存"
        android:icon="@android:drawable/ic_menu_save"/>

</menu>

 

菜单的点击也用以前的方式

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        
        switch (item.getItemId()) {
        case R.id.item1:
            Toast.makeText(this, "添加", 1000).show();
            break;
        case R.id.item2:
            Toast.makeText(this, "保存", 1000).show();
            break;

        default:
            break;
        }
        return false;
    }

 

上下文菜单

public class MainActivity extends Activity {
    private ListView lv;
    private String[] data = { "大连", "长春", "沈阳" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data));
        setContentView(lv);
        registerForContextMenu(lv);//注册上下文菜单
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("信息");
        menu.add(Menu.NONE, Menu.FIRST+1, 1, "ok");
        menu.add(Menu.NONE, Menu.FIRST+2, 2, "no");
    }
    

 上下文菜单实现的第二种方法,xml文件引用

package com.example.menudemo3;

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

public class MainActivity extends Activity {
    private ListView lv;
    private String[] data = { "大连", "长春", "沈阳" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data));
        setContentView(lv);
        registerForContextMenu(lv);//注册上下文菜单
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("添加信息");
        getMenuInflater().inflate(R.menu.contextmenu, menu);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.item1:
            Toast.makeText(this, "ok", 1000).show();
            break;

        
        }
        return super.onContextItemSelected(item);
    }

}

 子菜单

 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        SubMenu a = menu.addSubMenu("one");
        SubMenu b = menu.addSubMenu("two");
        a.add(menu.NONE, menu.FIRST+1, 1, "on");
        a.add(menu.NONE, menu.FIRST+2, 2, "ono");
        b.add(menu.NONE, menu.FIRST+3, 3, "ono");
        b.add(menu.NONE, menu.FIRST+4, 4, "on");
        return true;
    }

 子菜单通过xml文件获取

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        SubMenu a = menu.addSubMenu("one");
        SubMenu b = menu.addSubMenu("two");
        getMenuInflater().inflate(R.menu.one, a);
        getMenuInflater().inflate(R.menu.two, b);
        
        return true;
    }

 

posted @ 2015-10-21 14:41  aaddrrooiidd  阅读(134)  评论(0编辑  收藏  举报