Android_三种菜单介绍

Activity的主布局,activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        android:onClick="showPopup"
        android:src="@drawable/edit_indicator" />

</LinearLayout>

  列表视图ListView单行元素的布局,simple_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/ItemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/title_alert" />

    <TextView
        android:id="@+id/ItemTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/ItemText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

 
 菜单布局,main_menu.xml,三种菜单都是用这个菜单布局填充的

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

    <item
        android:id="@+id/setup"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/setup">
        <menu>
            <item
                android:id="@+id/setup_personal"
                android:title="@string/setup_personal"/>
            <item
                android:id="@+id/setup_global"
                android:title="@string/setup_global"/>
        </menu>
    </item>
    <item
        android:id="@+id/file"
        android:title="@string/file">
        <menu>
            <item
                android:id="@+id/file_new"
                android:title="@string/file_new"/>
            <item
                android:id="@+id/file_open"
                android:title="@string/file_open"/>
            <item
                android:id="@+id/file_save"
                android:title="@string/file_save"/>
        </menu>
    </item>
    <item
        android:id="@+id/edit"
        android:title="@string/edit">
        <menu>
            <item
                android:id="@+id/edit_copy"
                android:title="@string/edit_copy"/>
            <item
                android:id="@+id/edit_cut"
                android:title="@string/edit_cut"/>
            <item
                android:id="@+id/edit_paste"
                android:title="@string/edit_paste"/>
            <item
                android:id="@+id/edit_select"
                android:title="@string/edit_select"/>
        </menu>
    </item>
    <item
        android:id="@+id/help"
        android:title="@string/help">
        <menu>
            <item
                android:id="@+id/help_about"
                android:title="@string/help_about"/>
        </menu>
    </item>

</menu>

 MainActivity.java

package com.eric.mymenudemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
  private ListView mListView01;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 创建一个列表视图
    mListView01 = (ListView) this.findViewById(R.id.listView1);
    ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
    for (int i = 0; i < 10; i++) {
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("ItemImage", R.drawable.title_alert);
      map.put("ItemTitle", "Level" + i);
      map.put("ItemText", "这是列表视图第" + i + "行");
      listData.add(map);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, listData,
        R.layout.simple_list_item, new String[] { "ItemImage",
            "ItemTitle", "ItemText" }, new int[] { R.id.ItemImage,
            R.id.ItemTitle, R.id.ItemText });
    // 一个map是一行数据,此列表数组一共添加了10个map,也就是有10行数据,
    // 第3个参数是布局,表示一行,第4第5个参数把map和单行布局对应起来,一共10行
    mListView01.setAdapter(adapter);

    mListView01
        .setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1,
              int arg2, long arg3) {
            // TODO Auto-generated method stub
            // arg0是ListView,arg1是点击的那一行所在的View,在此为LinearLayout,arg2是在ListView中的位置,arg3是行数,在ListView中第几行
            String info = "你点击的是:\n" + "Parent:" + arg0.toString()
                + "\n" + "view:" + arg1.toString() + "\n"
                + "positon:" + Integer.toString(arg2) + ":"
                + arg0.getItemAtPosition(arg2).toString()
                + "\n" + "id:" + arg3;
            Toast.makeText(MainActivity.this, info,
                Toast.LENGTH_LONG).show();
          }
        });

    /*
     * mListView01 .setOnCreateContextMenuListener(new
     * View.OnCreateContextMenuListener() {
     * 
     * @Override public void onCreateContextMenu(ContextMenu menu, View v,
     * ContextMenuInfo menuInfo) { // TODO Auto-generated // method stub //
     * 系统返回三个参数,将要创建的菜单,注册菜单的View,在此为点击的那一行LinearLayout, MenuInflater
     * inflate = getMenuInflater(); // 将自定义的菜单填充到系统为这个View创建的menu中
     * inflate.inflate(R.menu.main_menu, menu); } });
     */

    this.registerForContextMenu(mListView01);
  }

  public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.main_menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

      @Override
      public boolean onMenuItemClick(MenuItem arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getItemId()) {
        case R.id.edit_copy:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.edit_cut:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.edit_paste:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.edit_select:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.file_new:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.file_open:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.file_save:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.help_about:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.setup_global:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        case R.id.setup_personal:
          MainActivity.this.setTitle(arg0.getTitle());
          Toast.makeText(MainActivity.this,
              "选项菜单:" + arg0.getTitle(), Toast.LENGTH_LONG)
              .show();
          return true;
        default:
          return false;
        }
      }
    });
  }

  public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenu.ContextMenuInfo menuInfo) {
    this.getMenuInflater().inflate(R.menu.main_menu, menu);
  }

  public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.edit_copy:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_cut:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_paste:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_select:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_new:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_open:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_save:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.help_about:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.setup_global:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.setup_personal:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .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.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.edit_copy:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_cut:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_paste:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.edit_select:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_new:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_open:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.file_save:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.help_about:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.setup_global:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    case R.id.setup_personal:
      this.setTitle(item.getTitle());
      Toast.makeText(this, "选项菜单:" + item.getTitle(), Toast.LENGTH_LONG)
          .show();
      break;
    }
    return super.onOptionsItemSelected(item);
  }

}

 效果:

选项菜单(option menu)

wang-jingyuan_20130614_050828

上下文菜单(context menu)

wang-jingyuan_20130614_050838

弹出菜单(PopMenu)

wang-jingyuan_20130614_050850

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值