android 菜單事件處理

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);
	}
	

}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *