代码添加菜单

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

/**
 * Class which shows how to handle menu item selection
 *
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {
	private static final int OK_MENU_ITEM = Menu.FIRST;
	private static final int SAVE_MENU_ITEM = OK_MENU_ITEM + 1;
	private static final int BACK_MENU_ITEM = SAVE_MENU_ITEM + 1;

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(0, OK_MENU_ITEM, 0, "Ok");
		menu.add(0, SAVE_MENU_ITEM, 0, "Save");
		menu.add(0, BACK_MENU_ITEM, 0, "Back");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		case OK_MENU_ITEM:
			showMsg("Ok");
			break;
		case SAVE_MENU_ITEM:
			showMsg("Save");
			break;
		case BACK_MENU_ITEM:
			showMsg("Back");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void showMsg(String msg) {
		Toast toast = Toast.makeText(Main.this, msg, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2);
		toast.show();
	}

}

 

你可能感兴趣的:(android,OS)