context menu
public class MenuTest_ContextActivity extends Activity { EditText et; Button btn; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et = (EditText)findViewById(R.id.editText1); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show(); } }); //위젯등록 registerForContextMenu(btn); }//end onCreate @Override public boolean onContextItemSelected(MenuItem item) { // Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show(); String str =""; switch( item.getItemId() ) { case 0: str = "AAA"; break; case 1: str = "BBB"; break; case 2: str = "CCC"; break; case 3: str = "DDD"; break; case 4: str = "EEE"; break; }//end switch Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //add( 그룹아이디, 아이템 아이디, 순서, 문자열 ) menu.add(1,0,0,"A"); menu.add(1,1,0,"B"); menu.add(1,2,0,"C"); menu.add(2,3,0,"D"); menu.add(2,4,0,"E"); super.onCreateContextMenu(menu, v, menuInfo); } }
options menu
public class MenuTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }//end onCreate // 맨 처음 실행시에만 한번 실행 public boolean onCreateOptionsMenu(Menu menu) { // 아이템 지정 MenuInflater inf = getMenuInflater(); inf.inflate(R.menu.mymenu, menu); Log.i("xxx", "onCreateOptionsMenu"); return super.onCreateOptionsMenu(menu); }//end onCreateOptionsMenu //로그인 메서드 boolean loginFlag=false; public void login() { loginFlag=true; } public void logout() { loginFlag=false; } @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show(); switch(item.getItemId()) { case R.id.item01: break; case R.id.item06: if(loginFlag==false) { login(); }else { logout(); } break; }//end switch return super.onOptionsItemSelected(item); }//end onOptionsItemSelected //메뉴 버튼 누를때마다 매번 실행 public boolean onPrepareOptionsMenu(Menu menu) { Log.i("xxx", "onPrepareOptionsMenu"); if(loginFlag==true) { menu.getItem(5).setTitle("로그아웃"); } else { menu.getItem(5).setTitle("로그인"); } return super.onPrepareOptionsMenu(menu); } }