android 菜单事件处理

时间:2022-11-26 18:11:55
package com.example.wenandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView; public class OptionMenuDemo extends Activity {
private static final int MENU_ITEM_COUNTER=Menu.FIRST;
private TextView mtextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionmenu);
mtextview=(TextView)findViewById(R.id.mtextview);
mtextview.setText("要大一点才能看清楚");
registerForContextMenu(mtextview);
} @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { menu.add(0,MENU_ITEM_COUNTER+1,0,"白色");
menu.add(0, MENU_ITEM_COUNTER+2, 0, "黄色");
menu.add(0, MENU_ITEM_COUNTER+3, 0, "蓝色"); super.onCreateContextMenu(menu, v, menuInfo);
} @Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case MENU_ITEM_COUNTER+1:
mtextview.setBackgroundResource(R.drawable.first);
break;
case MENU_ITEM_COUNTER+2:
mtextview.setBackgroundResource(R.drawable.second);
break;
case MENU_ITEM_COUNTER+3:
mtextview.setBackgroundResource(R.drawable.third);
break;
}
return super.onContextItemSelected(item);
} }