티스토리 뷰

안드로이드/프로그래밍

메뉴

에어버스 2015. 8. 26. 13:10

옵션메뉴, 컨텍스트메뉴

A. 옵션메뉴
1. 메뉴만들기

1
2
3
4
5
6
7
8
9
10
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   SubMenu etc = menu.addSubMenu("계좌관리");
   etc.add(010"계좌등록");
   etc.add(020"계좌수정");
   etc.add(030"계좌삭제");
   getMenuInflater().inflate(R.menu.menu_stock, menu);
   return true;
}
cs

2. 메뉴 이벤트 처리
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
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
 
    int id = item.getItemId();
 
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    switch(id) {
        case 1:
            Toast.makeText(this"등록", Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(this"수정", Toast.LENGTH_SHORT).show();
            break;
        case 3:
            Toast.makeText(this"삭제", Toast.LENGTH_SHORT).show();
            break;
    }
    return super.onOptionsItemSelected(item);
}
cs


B. 컨텍스트 메뉴
1. 메뉴만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
 
        if(!m_b주문진행출력상태) // 주문진행상태가 아닌경우 무시
            return;
        if(m_b수정모드) {
            menu.add(01, Menu.NONE, "수정")
                .setEnabled(false);
            menu.add(02, Menu.NONE, "삭제");
            menu.add(03, Menu.NONE, "수정취소");
        }
        else {
            menu.add(01, Menu.NONE, "수정");
            menu.add(02, Menu.NONE, "삭제");
            menu.add(03, Menu.NONE, "수정취소")
                .setEnabled(false);
        }
    }
cs


2. 메뉴 이벤트처리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
    public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);
 
        AdapterView.AdapterContextMenuInfo menuInfo;
        menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int index = menuInfo.position; // 리스트뷰에서 선택된 종목의 인덱스번호
        switch(item.getItemId()) {
            case 1// 수정
                m_b수정모드 = true;                
                return true;
            case 2// 삭제
                종목삭제요청(index);                
                return true;
            case 3// 수정취소
                버튼초기화();
                return true;
        }
        return false;
    }
cs
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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