본문 바로가기

Programming/Android

[Android] #9 OptionMenu(단말기 menu 버튼)

1. 옵션메뉴 항목 설정 : res/menu/quick.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
          android:id="@+id/home"
          android:title="Home"
          android:orderInCategory="1"></item>
    <item
          android:id="@+id/exit"
          android:title="Exit"
          android:orderInCategory="2"></item>
    <item
          android:id="@+id/cook1"
          android:title="Cook1"
          android:orderInCategory="3"></item>
    <item
          android:id="@+id/cook2"
          android:title="Cook2"
          android:orderInCategory="4"></item>
    <item
          android:id="@+id/cook3"
          android:title="Cook3"
          android:orderInCategory="5"></item>
</menu>



2. 옵션 메뉴 구현

public class BlogTest extends Activity {
    .........
    // OptionMenu
    public boolean onCreateOptionsMenu(Menu menu){
          getMenuInflater().inflate(R.menu.quick, menu);
          return true;
    }

    // OptionMenu click event
    public boolean onOptionsItemSelected(MenuItem item) {
           switch (item.getItemId()) {
               case R.id.exit:
                       finish();
                       return true;
               case R.id.cook1:
                       startActivity(new Intent(this, Sample00.class));
                       return true;
               case R.id.cook2:
                       startActivity(new Intent(this, Sample01.class));
                       return true;
            }
            return false;
     }
}