ANDROID OptionMenu 菜单列表

package com.app.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView.FindListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends Activity {
	/*定义菜单ID	 */
	private static final int M_CHANGE_FIRST=Menu.FIRST;
	private static final int M_CHANGE_SECOND=Menu.FIRST+1;
	private static final int M_CHANGE_HELP=Menu.FIRST+2;  
	
	/*创建菜单 重写onCreateOptionMenu 方法	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		int idGroup1=3;//创建menu 群组ID
		//创建menu 顺序ID
		int orderMenuItem1 = Menu.NONE;
		int orderMenuItem2 = Menu.NONE+1;
		int orderMenuItem3 = Menu.NONE+2;
		menu.add(idGroup1,M_CHANGE_FIRST,orderMenuItem1,"切换第1张图片").setIcon(R.drawable.img1);
		menu.add(idGroup1,M_CHANGE_SECOND,orderMenuItem2,"切换第2张图片").setIcon(R.drawable.img2);
		menu.add(idGroup1, M_CHANGE_HELP,orderMenuItem3,"帮助|HELP").setIcon(R.drawable.img3); 
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	/*选择菜单 重写onOptionItemSelected方法*/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id= item.getItemId();//获得menu菜单的id
		//获得ImageView对象
		ImageButton  imageView = (ImageButton) findViewById(R.id.imageButton);		
		 
		switch (id) {
			case M_CHANGE_FIRST :				
				imageView.setImageDrawable(getResources().getDrawable(R.drawable.jpg1 ));
				break;
			case M_CHANGE_SECOND :				
				imageView.setImageDrawable(getResources().getDrawable(R.drawable.jpg2));
				break;
			case M_CHANGE_HELP :				
				Intent intent = new Intent(MainActivity.this,HelpActivity.class);
				startActivity(intent);
				break;

			default :
				break;
		}
		return super.onOptionsItemSelected(item);
	}

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

posted @ 2013-07-05 16:32  一个学渣  阅读(185)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved