8.1 菜单-OptionMenu
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1;
//加入菜单项
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.add(0, MENU_ABOUT, 0, "关于Yanger");
menu.add(0, MENU_QUIT, 1, "退出");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ABOUT: {
Toast.makeText(Main.this, "她是一个没有长大的孩子!她生活在南方的一座美丽的滨江小城!",
Toast.LENGTH_LONG).show();
break;
}
case MENU_QUIT: {
finish();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
8.2 快捷菜单-ContextMenu
package com.wwtiot.yanger;
import android.app.Activity;
import android.graphics.Color;
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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
TextView textShow01;
TextView textShow02;
LinearLayout myLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取资源文件中的界面组件
textShow01 = (TextView) findViewById(R.id.textShow01);
textShow02 = (TextView) findViewById(R.id.textShow02);
myLayout = (LinearLayout) findViewById(R.id.myLayout);
registerForContextMenu(textShow01);
registerForContextMenu(textShow02);
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.add(0, MENU_ABOUT, 0, "关于Yanger");
menu.add(0, MENU_QUIT, 1, "退出");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ABOUT: {
Toast.makeText(Main.this, "她是一个没有长大的孩子!她生活在南方的一座美丽的滨江小城!",
Toast.LENGTH_LONG).show();
break;
}
case MENU_QUIT: {
finish();
break;
}
}
return super.onOptionsItemSelected(item);
}
// 定义单一选项的常数
protected static final int MENU_BACKCOLOR = Menu.FIRST;
protected static final int MENU_SMALLSIZE = Menu.FIRST + 1;
protected static final int MENU_LARGESIZE = Menu.FIRST + 2;
// 加入ContextMenu的选项
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == textShow01)
menu.add(0, MENU_BACKCOLOR, 0, "设置背景颜色");
else if (v == textShow02) {
menu.add(0, MENU_SMALLSIZE, 1, "较小字体");
menu.add(0, MENU_LARGESIZE, 2, "较大字体");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_BACKCOLOR: {
myLayout.setBackgroundColor(Color.BLACK);
break;
}
case MENU_SMALLSIZE: {
textShow01.setTextSize(12);
textShow02.setTextSize(12);
break;
}
case MENU_LARGESIZE: {
textShow01.setTextSize(24);
textShow02.setTextSize(24);
break;
}
}
return super.onContextItemSelected(item);
}
}
main.xml: