天天看點

Android中Option menu和Context menu的使用(十六)

       Menu由兩種形式,Option menu和Context menu。前者是按下裝置的Menu硬按鈕彈出,後者是長按widget彈出。

1、Option Menu:當我們按下Menu的硬體按鈕時,Option Menu将被觸發顯示,最多可以顯示6個選項的icon菜單,

如果選項多于6個,第6個選項顯示為“More“,點選可以進入擴充菜單。

步驟1:建立Menu

步驟2:Menu觸發比較簡單,Activity在Memu後會觸發onOptionsItemSelected()進行處理。               

步驟3:每次顯示menu時根據實際的情況進行适配

步驟4:子菜單,Android支援二級菜單,但是不支援三級等多級菜單。子菜單設定如下,在onCreateOptionsMenu(),

2、Context Menu:Context Menu是使用者手指長按某個View觸發的菜單。

步驟1:為某個view注冊ContextMenu,例如在我們的例子中,在onCreate()中對整個ListView進行處理:

步驟2:建立ContextMenu,通過Override onCreateContextMenu()來建立Context Menu。

步驟3:點選菜單觸發函數,觸發onContextItemSelected()。

代碼片段:

package com.itarchy.menu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * 
 * @author:itarchy
 * @E-mail:[email protected]
 * @data:2015-11-19上午10:26:37
 * @version:
 * @Des:
 */
public class HomeActivity extends Activity {

	private Button btn_op;
	private Button btn_Ct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btn_op = (Button) this.findViewById(R.id.btn_op);
		btn_op.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(HomeActivity.this,
						OptionMenuActivity.class));

			}
		});
		btn_Ct = (Button) this.findViewById(R.id.btn_ct);
		btn_Ct.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(HomeActivity.this,
						ContextMenuActivity.class));

			}
		});

	}
}
           
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OptionMenuActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="菜單"
        android:textSize="30sp" />

    <Button
        android:id="@+id/btn_op"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:text="選項菜單和子菜單"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_ct"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_op"
        android:text="上下文菜單"
        android:textSize="20sp" />

</RelativeLayout>           
package com.itarchy.menu;

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 OptionMenuActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.op_main);
	}

	// 建立選菜單
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 1、方式一、通過一個MenuInflater把在XML布局檔案中定義好的菜單樣式應用在菜單上
		/* getMenuInflater().inflate(R.menu.main, menu); */
		// 2、方式二:
		// 直接在Java代碼中使用add()方法逐個添加菜單項
		menu.add(Menu.NONE, 0, Menu.NONE, "設定").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 1, Menu.NONE, "添加").setIcon(R.drawable.qq_kong);
		// 給第三項添加子菜單
		SubMenu subMenu = menu.addSubMenu(Menu.NONE, 2, Menu.NONE, "子菜單1")
				.setIcon(R.drawable.qq);
		subMenu.setHeaderIcon(R.drawable.qq_kong);
		subMenu.setHeaderTitle("子菜單标題");
		subMenu.add(Menu.NONE, 112, Menu.NONE, "子菜單1");
		subMenu.add(Menu.NONE, 113, Menu.NONE, "子菜單2");
		subMenu.add(Menu.NONE, 114, Menu.NONE, "子菜單3");
		subMenu.add(Menu.NONE, 115, Menu.NONE, "子菜單4");

		menu.add(Menu.NONE, 3, Menu.NONE, "圖庫").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 4, Menu.NONE, "相機").setIcon(R.drawable.qq_kong);
		menu.add(Menu.NONE, 5, Menu.NONE, "音樂").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 6, Menu.NONE, "藍牙").setIcon(R.drawable.qq_kong);
		menu.add(Menu.NONE, 7, Menu.NONE, "個人中心");
		return true;
	}

	// 點選菜單的items時執行
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Toast.makeText(getApplication(),
				item.getTitle() + "," + (item.getItemId() + ""), 100).show();
		switch (item.getItemId()) {
		case 0:
			// 處理"設定"
			break;
		case 1:
			// 處理"添加"
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
           
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OptionMenuActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:text="選項菜單和子菜單" />

</RelativeLayout>
           
package com.itarchy.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ContextMenuActivity extends Activity {

	private TextView tvMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ct_main);

		tvMenu = (TextView) this.findViewById(R.id.title);
		this.registerForContextMenu(tvMenu);

	}

	// 建立上下文菜單
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		menu.add(Menu.NONE, 0, Menu.NONE, "設定").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 1, Menu.NONE, "添加").setIcon(R.drawable.qq_kong);

		// 給第三項添加子菜單
		SubMenu subMenu = menu.addSubMenu(Menu.NONE, 2, Menu.NONE, "子菜單1")
				.setIcon(R.drawable.qq);
		subMenu.setHeaderIcon(R.drawable.qq_kong);
		subMenu.setHeaderTitle("子菜單标題");
		subMenu.add(Menu.NONE, 112, Menu.NONE, "子菜單1");
		subMenu.add(Menu.NONE, 113, Menu.NONE, "子菜單2");
		subMenu.add(Menu.NONE, 114, Menu.NONE, "子菜單3");
		subMenu.add(Menu.NONE, 115, Menu.NONE, "子菜單4");

		menu.add(Menu.NONE, 3, Menu.NONE, "圖庫").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 4, Menu.NONE, "相機").setIcon(R.drawable.qq_kong);
		menu.add(Menu.NONE, 5, Menu.NONE, "音樂").setIcon(R.drawable.qq);
		menu.add(Menu.NONE, 6, Menu.NONE, "藍牙").setIcon(R.drawable.qq_kong);
		menu.add(Menu.NONE, 7, Menu.NONE, "個人中心");

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

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Toast.makeText(getApplication(),
				item.getTitle() + "," + (item.getItemId() + ""), 100).show();
		switch (item.getItemId()) {
		case 0:
			// 處理"設定"
			break;
		case 1:
			// 處理"添加"
			break;
		}
		return super.onContextItemSelected(item);
	}
}
           
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OptionMenuActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:text="上下文菜單" />

</RelativeLayout>
           
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itarchy.menu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itarchy.menu.HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.itarchy.menu.OptionMenuActivity" />
        <activity android:name="com.itarchy.menu.ContextMenuActivity" />
    </application>

</manifest>