Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android SubMenu的使用

android SubMenu的使用

編輯:關於Android編程



public class MainActivity extends Activity {

//定義字體大小菜單項的標識
final int FONT_10 = 0x111;
final int FONT_12 = 0x112;
final int FONT_14 = 0x113;
final int FONT_16 = 0x114;
final int FONT_18 = 0x115;
//定義普通菜單項的標識
final int PLAIN_ITEM = 0x11b;
//定義字體顏色菜單項標識
final int FONT_RED = 0x116;
final int FONT_BLUE = 0x117;
final int FONT_GREEN = 0x118;
private EditText edit;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontMenu = menu.addSubMenu("字體大小");
fontMenu.setIcon(R.drawable.ic_launcher);
fontMenu.setHeaderIcon(R.drawable.fy_d_kf);
fontMenu.setHeaderTitle("選擇字體大小");
fontMenu.addSubMenu(0, FONT_10, 0, "10號字體");
fontMenu.addSubMenu(0, FONT_12, 0, "12號字體");
fontMenu.addSubMenu(0, FONT_14, 0, "14號字體");
fontMenu.addSubMenu(0, FONT_16, 0, "16號字體");
fontMenu.addSubMenu(0, FONT_18, 0, "18號字體");

menu.addSubMenu(0, PLAIN_ITEM, 0, "普通菜單項");

SubMenu colorMenu = menu.addSubMenu("字體顏色");
colorMenu.setIcon(R.drawable.fy_d_kf_s);
colorMenu.setHeaderIcon(R.drawable.fy_d_more_s);
colorMenu.setHeaderTitle("選擇文字顏色");
colorMenu.addSubMenu(0, FONT_RED, 0, "紅色");
colorMenu.addSubMenu(0, FONT_GREEN, 0, "綠色");
colorMenu.addSubMenu(0, FONT_BLUE, 0, "藍色");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
edit.setTextSize(10*2);
break;
case FONT_12:
edit.setTextSize(12*2);
break;
case FONT_14:
edit.setTextSize(14*2);
break;
case FONT_16:
edit.setTextSize(16*2);
break;
case FONT_18:
edit.setTextSize(18*2);
break;
case FONT_GREEN:
edit.setTextColor(Color.GREEN);
break;
case FONT_BLUE:
edit.setTextColor(Color.BLUE);
break;
case FONT_RED:
edit.setTextColor(Color.RED);
break;
case PLAIN_ITEM:
Toast.makeText(this, "nin點擊的普通菜單", Toast.LENGTH_LONG).show();
break;
}
return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText1);
}


}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved