メニューの簡単な入門
4206 ワード
public class WidgetActivity extends Activity {
/** Called when the activity is first created. */
private final String TAG = "WidgetActivity...";
private Menu myMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
myMenu = menu;
addRegularMenuItems(menu);
addSeconderyMeunItems(menu);
return true;
}
private void addSeconderyMeunItems(Menu menu) {
// TODO Auto-generated method stub
int base = Menu.CATEGORY_SECONDARY;
menu.add(base, base+1, base+1, "sec. item 1");
menu.add(base, base+2, base+2, "sec. item 2");
menu.add(base, base+3, base+3, "sec. item 3");
menu.add(base, base+3, base+3, "sec. item 4");
menu.add(base, base+4, base+4, "sec. item 5");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//
Log.i(TAG, item.getItemId()+ "" );
if(item.getItemId() == 1){
appendText("
Hello...");
}else if(item.getItemId() ==2){
appendText("
item2");
} else if( item.getItemId() == 3){
emptyText();
}else if( item.getItemId() == 4){
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
} else if(item.getItemId() == 5 ){
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
}
else if( item.getItemId()==6){
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
}else if(item.getItemId() == 7){
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
}else if(item.getItemId() == 8 ){
this.appendMenuItemText(item);
this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
}else if(item.getItemId()==9){
this.appendMenuItemText(item);
this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
}
else
this.appendMenuItemText(item);
return super.onOptionsItemSelected(item);
}
private void emptyText() {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText("");
}
private void appendMenuItemText(MenuItem item) {
// TODO Auto-generated method stub
String title = item.getTitle().toString();
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(textView.getText() + "
" + title);
}
private void appendText(String string) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(tv.getText() + string);
}
private void addRegularMenuItems(Menu menu){
int base = Menu.FIRST;
menu.add(base, base, base, "append");
menu.add(base, base+1, base+1, "item2");
menu.add(base, base + 2, base + 2, "clear");
menu.add(base,base+3,base+3,"hide secondery");
menu.add(base, base+4, base+4, "show secomdery");
menu.add(base, base+5, base+5, "enable scondery");
menu.add(base, base+6, base+6, "disable secondery");
menu.add(base, base+7, base+7, "check secondery");
menu.add(base, base+8, base+8, "uncheck secondery");
}
}