1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package com.formation.menu;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity implements OnLongClickListener {
/** Called when the activity is first created. */
LinearLayout view;
int checked1000=0;
int checked1001=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = ((LinearLayout)this.findViewById(R.id.view1));
view.setOnLongClickListener(this);
view.setOnCreateContextMenuListener(this);
}
private void onCreateMenu(Menu menu){
SubMenu m = menu.addSubMenu(0, 2000, 0, "Sous menu 1");
m.add(0,1000,0,"Menu 1").setCheckable(true);
m.add(0,1001,0,"Menu 2").setCheckable(true);
m.setIcon(android.R.drawable.ic_menu_call);
m.setHeaderIcon(android.R.drawable.ic_menu_agenda);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
onCreateMenu(menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1000:
if (item.isChecked()) {
checked1000=0;
} else {
checked1000=1;
}
Toast.makeText(this,"Menu 1",1000).show();
break;
case 1001:
if (item.isChecked()) {
checked1001=0;
} else {
checked1001=1;
}
Toast.makeText(this,"Menu 2",1000).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(2000).setTitle(""+SystemClock.elapsedRealtime());
if (checked1000==1) {
menu.findItem(1000).setChecked(true);
} else {
menu.findItem(1000).setChecked(false);
}
if (checked1001==1) {
menu.findItem(1001).setChecked(true);
} else {
menu.findItem(1001).setChecked(false);
}
return super.onPrepareOptionsMenu(menu);
}
public boolean onLongClick(View v) {
v.showContextMenu();
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
onCreateMenu(menu);
menu.findItem(2000).setTitle(""+SystemClock.elapsedRealtime());
if (checked1000==1) {
menu.findItem(1000).setChecked(true);
} else {
menu.findItem(1000).setChecked(false);
}
if (checked1001==1) {
menu.findItem(1001).setChecked(true);
} else {
menu.findItem(1001).setChecked(false);
}
super.onCreateContextMenu(menu, v, menuInfo);
menu.close();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1000:
Toast.makeText(this,"Context Menu 1",1000).show();
if (item.isChecked()) {
checked1000=0;
} else {
checked1000=1;
}
break;
case 1001:
Toast.makeText(this,"Context Menu 2",1000).show();
if (item.isChecked()) {
checked1001=0;
} else {
checked1001=1;
}
break;
}
return super.onContextItemSelected(item);
}
} |
Partager