2011年6月21日星期二

子菜單(SubMenu)

使用子菜單(SubMenu)的工作示例:

子菜單(SubMenu)

package com.AndroidMenu;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class AndroidMenuActivity extends Activity {

final static int OPTION_ID_1 = 1;
final static int OPTION_ID_2 = 2;
final static int OPTION_ID_3 = 3;
final static int OPTION_ID_4 = 4;
final static int OPTION_ID_5 = 5;

final static int OPTION_ID_6 = 6;
final static int OPTION_ID_6_1 = 61;
final static int OPTION_ID_6_2 = 62;
final static int OPTION_ID_6_3 = 63;

/** Called when the activity is first created. */
@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);

//menu.add(Group, item id, order, title);
menu.add(0, OPTION_ID_1, 0, "Option 1");
menu.add(0, OPTION_ID_2, 1, "Option 2");
menu.add(0, OPTION_ID_3, 2, "Option 3");
menu.add(0, OPTION_ID_4, 3, "Option 4");
menu.add(0, OPTION_ID_5, 4, "Option 5");

SubMenu subMenu6 = menu.addSubMenu(0, OPTION_ID_5, 5, "SubMenu");
subMenu6.add(0, OPTION_ID_6_1, 0, "SubMenu6.1");
subMenu6.add(0, OPTION_ID_6_2, 0, "SubMenu6.2");
subMenu6.add(0, OPTION_ID_6_3, 0, "SubMenu6.3");

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

switch(item.getItemId()){
case OPTION_ID_1:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_2:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_3:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_4:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_5:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;

}

return true;
}


}



相關文章:
- 選項菜單(OptionsMenu)

1 則留言:

  1. 請問如果要以menu配合submenu開啟SD files要如何修改呢

    回覆刪除