第一个androidTest menu

package com.example.androidtest;


import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;


public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

menu.add(0, 1, 1, "苹果");

menu.add(0, 2, 2, "香蕉");

return super.onCreateOptionsMenu(menu);


}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

if (item.getItemId() == 1) {

Toast t = Toast.makeText(this, "你选的是苹果", Toast.LENGTH_SHORT);

t.show();

} else if (item.getItemId() == 2) {

Toast t = Toast.makeText(this, "你选的是香蕉", Toast.LENGTH_SHORT);

t.show();

}

return true;

}


}


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