右クリックメニューおよび選択
2353 ワード
public class Context extends Activity {
/** Called when the activity is first created. */
String checkedItem;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerForContextMenu(findViewById(R.id.primaryText));
registerForContextMenu(findViewById(R.id.secondaryText));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.primaryText)
{
menu.setHeaderTitle("Primary context");
addMenuItem(menu, "Primary 1", true);
addMenuItem(menu, "Primary 2", true);
addMenuItem(menu, "Primary 3", false);
}
else
{
menu.setHeaderTitle("Secondary context");
addMenuItem(menu, "Secondary 1", true);
addMenuItem(menu, "Secondary 2", true);
addMenuItem(menu, "Secondary 3", false);
}
}
private MenuItem addMenuItem(ContextMenu menu, String itemName, boolean checkable) {
MenuItem item = menu.add(itemName);
item.setCheckable(checkable);
if (itemName.equals(checkedItem))
{
item.setChecked(true);
}
return item;
// TODO Auto-generated method stub
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
TextView statusText = (TextView) findViewById(R.id.statusView);
if (item.isCheckable() == true)
{
// Visually display what is checked.
checkedItem = item.getTitle().toString();
}
statusText.setText("Last item " + item.getTitle());
return true;
}
}