2011. 8. 31. 17:22

안드로이드 메뉴(Menu) 만들기


XML은 Linear Layout에 TextView만 배치하면 됩니다.

package kr.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidMenu extends Activity {
 private TextView text ;
 
    /-* Called when the activity is first created. *-/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text = (TextView)findViewById(R.id.text); 
    } // onCreate()
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  menu.add(0, 0, 0, "My Menu1") ;
  menu.add(0, 1, 0, "My Menu2") ;
  menu.add(0, 2, 0, "My Menu3") ;
  
  return super.onCreateOptionsMenu(menu);
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  switch(item.getItemId()) {
  case 0 :
   text.setText("안녕하세요") ;
   break ;
   
  case 1 :
   text.setText("반갑습니다") ;
   break ;
   
  case 2 :
   text.setText("환영합니다") ;
   break ;
  }
    
  return super.onOptionsItemSelected(item);
 }
    
} // class

Trackback 0 Comment 0