Android入门之Menu动态修改(纯java)

     

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;

public class MenuModeTest extends Activity {
	
	private TextView tv=null;
	private MenuItem mi1=null;
	private MenuItem mi2=null;
	private MenuItem mi3=null;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv=new TextView(this);
        tv.setText("动态菜单,菜单选项可修改");
        setContentView(tv);
    }
    
    public boolean onCreateOptionsMenu(Menu menu){
    	mi1=menu.add(0,1, 1, "菜单1");
    	mi2=menu.add(0,2, 2, "菜单2");
    	mi3=menu.add(0,3, 3, "修改");
		return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case 1:
    		tv.setText("你按下了菜单1");break;
    	case 2:
    		tv.setText("你按下了菜单2");break;
    	case 3:
    		modeMenu();break;
    	default: tv.setText("xxx");
    	}
		return false;
    }
    
    public void modeMenu(){
    	mi1.setIcon(R.drawable.icon);
    	mi2.setTitle("2菜单");
    	mi3.setEnabled(false);
    	
    	/*菜单选项操作*/
    	/*
    	mi1.setEnabled(enabled)
    	mi1.setIcon(icon)
    	mi1.setTitle(title)
    	*/

    	/*菜单整体主要操作*/
    	/*
    	menu.add()
    	menu.addSubMenu()	
    	menu.removeGroup(groupId)
    	menu.removeItem(id)
    	menu.setGroupEnabled(group, enabled)
    	menu.setGroupVisible(group, visible)
    	*/
    }
}

你可能感兴趣的:(Android)