option Menu 和 contextMenu

option Menu

1. create id item:=>

privatestaticfinalintMTEM1=Menu.FIRST;

privatestaticfinalintMTEM2=Menu.FIRST+1;

privatestaticfinalintMTEM3=Menu.FIRST+2;

2. override onCreateOptionsMenu

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

menu.add(0,MTEM1,0,“RED”);

menu.add(0,MTEM2,0,“GREEN”);

menu.add(0,MTEM3,0,“BLACK”);

//return super.onCreateOptionsMenu(menu);

returntrue;

}

 

3.overide onOptionsItemSelected

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

//return super.onOptionsItemSelected(item);

switch (item.getItemId()) {

caseMTEM1:

myTV.setBackgroundColor(Color.RED);

break;

caseMTEM2:

myTV.setBackgroundColor(Color.GREEN);

break;

caseMTEM3:

myTV.setBackgroundColor(Color.BLACK);

break;

}

returntrue;

}

 

 

contextMenu

1. create id item

privatestaticfinalintITEM1=Menu.FIRST;

privatestaticfinalintITEM2=Menu.FIRST+1;

privatestaticfinalintITEM3=Menu.FIRST+2;

 

2.override

@Override onCreateContextMenu

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

// TODO Auto-generated method stub

menu.add(0,ITEM1,0,“RED”);

menu.add(0,ITEM2,0,“GREEN”);

menu.add(0,ITEM3,0,“BLACK”);

 

}

 

3. override  onContextItemSelecte

@Override

public boolean onContextItemSelected(MenuItem item) {

// TODO Auto-generated method stub

switch (item.getItemId()) {

caseITEM1:

myTV.setBackgroundColor(Color.RED);

break;

caseITEM2:

myTV.setBackgroundColor(Color.GREEN);

break;

caseITEM3:

myTV.setBackgroundColor(Color.BLACK);

break;

 

 

}

returntrue;

}

 

 

complet source code

package com.example.test2;

 

import com.example.test2.R;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ContextMenu.ContextMenuInfo;

import android.widget.TextView;

 

public class MainActivity extends Activity {

privatestaticfinalintITEM1=Menu.FIRST;

privatestaticfinalintITEM2=Menu.FIRST+1;

privatestaticfinalintITEM3=Menu.FIRST+2;

 

privatestaticfinalintMTEM1=Menu.FIRST;

privatestaticfinalintMTEM2=Menu.FIRST+1;

privatestaticfinalintMTEM3=Menu.FIRST+2;

private TextView myTV;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myTV=(TextView) findViewById(R.id.textview1);

registerForContextMenu(myTV);

}

 

@Override

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

// TODO Auto-generated method stub

menu.add(0,ITEM1,0,“RED”);

menu.add(0,ITEM2,0,“GREEN”);

menu.add(0,ITEM3,0,“BLACK”);

 

}

 

//OPTION MENU

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

menu.add(0,MTEM1,0,“RED”);

menu.add(0,MTEM2,0,“GREEN”);

menu.add(0,MTEM3,0,“BLACK”);

//return super.onCreateOptionsMenu(menu);

returntrue;

}

 

@Override

 

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

//return super.onOptionsItemSelected(item);

switch (item.getItemId()) {

caseMTEM1:

myTV.setBackgroundColor(Color.RED);

break;

caseMTEM2:

myTV.setBackgroundColor(Color.GREEN);

break;

caseMTEM3:

myTV.setBackgroundColor(Color.BLACK);

break;

}

returntrue;

}

 

 

 

@Override

public boolean onContextItemSelected(MenuItem item) {

// TODO Auto-generated method stub

switch (item.getItemId()) {

caseITEM1:

myTV.setBackgroundColor(Color.RED);

break;

caseITEM2:

myTV.setBackgroundColor(Color.GREEN);

break;

caseITEM3:

myTV.setBackgroundColor(Color.BLACK);

break;

 

 

}

returntrue;

}

 

}

 

 

 

Leave a Comment