当前位置: 首页 > 工具软件 > FastFind Menu > 使用案例 >

Android Menu(Context Menu,Options Menu,SubMenu)实例

盖和洽
2023-12-01
 
package com.cocol.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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 MenuActivity  extends Activity{
    private final int MENU_ADD=1,MENU_SEND=2,MENU_DEL=3;
    private final int GROUP_DEFAULT=0,GROUP_DEL=1;
    private final int ID_DEFAULT=0;
    private final int ID_TEXT1=1,ID_TEXT2=2,ID_TEXT3=3;
    private String[] choices={"Press Me","Try Again","Change Me"};
    
    private static int itemNum=0;
    private static TextView bv;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.menu_layout);
		bv=(TextView)findViewById(R.id.focus_text);
		
		this.registerForContextMenu(bv);
		
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
		 ContextMenuInfo menuInfo) {
		Log.i("MenuActivity","onCreateContextMenu");
		if(v.getId()==R.id.focus_text){
			SubMenu textMenu=menu.addSubMenu("Change Text");
			textMenu.add(0,ID_TEXT1,0,choices[0]);
			textMenu.add(0,ID_TEXT2,0,choices[1]);
			textMenu.add(0,ID_TEXT3,0,choices[2]);
			menu.add(0,ID_DEFAULT,0,"Original Text");
			
		}
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("MenuActivity","onCreateOptionsMenu");
		menu.add(GROUP_DEFAULT, MENU_ADD, 0, "Add").setIcon(R.drawable.icon);
		menu.add(GROUP_DEFAULT,MENU_SEND,0,"Send");
		menu.add(GROUP_DEL,MENU_DEL,0,"Delete");
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case MENU_ADD:
			createNote();
			break;
		case MENU_SEND:
			sendNote();
			break;
		case MENU_DEL:
			deleteNote();
			break;
		}
		return true;
	}
	
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		Log.i("MenuActivity","onPrepareOptionsMenu");
		if(itemNum>0){
			menu.setGroupVisible(GROUP_DEL, true);
		}else{
			menu.setGroupVisible(GROUP_DEL, false);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case ID_DEFAULT:
			bv.setText("Hello");
			return true;
		case ID_TEXT1:
		case ID_TEXT2:
		case ID_TEXT3:
		   bv.setText(choices[item.getItemId()-1]);
		   return true;
		}
		return super.onContextItemSelected(item);
	}

	void createNote(){
		itemNum++;
	}
	
	void deleteNote(){
		itemNum--;
	}
	
	void sendNote(){
		Toast.makeText(this, "Item: "+itemNum,Toast.LENGTH_LONG).show();
	}
		
	
}

 

总结调用顺序:

       点击“Menu" 键时,调用 onCreateOptionsMenu----> onPrepareOptionsMenu.之后,再点击‘Menu" 键时,onCreateOptionsMenu不会被调用,但onPrepareOptionsMenu会被调用,在这里可以动态的改变菜单。

   至于ContextMenu,每次长按view. 弹出菜单栏,都会调用onCreateContextMenu.

 类似资料: