안드로이드 메뉴 옵션 예제 소스입니다.


예제소스

package com.example.h5bak_optionmenu;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("아래 메뉴를 눌러보세요!!(호박머리)");
        setContentView(tv);
    }
 
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0,1,0,"메뉴A"); //int groupId, int itemId, int order, CharSequence title
        menu.add(0,2,0,"메뉴B");
        SubMenu etc = menu.addSubMenu("etc");
        etc.add(0,3,0,"etc A");
        etc.add(0,4,0,"etc B");
        return true;
    }
 
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            Toast.makeText(this,"메뉴A",1).show();
            return true;
        case 2:
            Toast.makeText(this,"메뉴B",1).show();
            return true;
        case 3:
            Toast.makeText(this,"etc A",1).show();
            return true;
        case 4:
            Toast.makeText(this,"etc B",1).show();
            return true;
        }
        return false;
    }
}


실행결과

Posted by 준콩ol 준콩ol

댓글을 달아 주세요