美图欣赏 | 设为首页 | 加入收藏 | 网站地图

当前位置:电脑中国 > 编程 > 移动开发 >

android开发之选项菜单,上下文菜单,子菜单的设置

2017-08-08 10:23|来源:未知 |作者:dnzg |点击:
android开发之选项菜单,上下文菜单,子菜单的设置,设置选项菜单用到的是OnCreateOptionMenu()

 

上下文菜单是OnCreateContextMenu(),然后在你想要的控件上注册上下文菜单

子菜单则是使用SubMenu 利用 menu.addSubMenu, submenu.add();

比较简单,直接看代码吧

?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class StartGameActivity extends Activity {
    static final int GAME_OPTION=1;
    static final int HERO_RECORD=2;
 
    private TextView tv_load;
    private Button btn_stargame,btn_setting;
    private ProgressBar pb_load;
    int progress = 0;
    Timer timer=new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            if (progress<100){
                progress++;
                handler.sendEmptyMessage(0x111);
            }else {
                handler.sendEmptyMessage(0x222);
                task.cancel();
                timer.cancel();
            }
 
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startgame);
        tv_load = (TextView)findViewById(R.id.tv_load);
        btn_stargame = (Button)findViewById(R.id.btn_startgame);
        pb_load = (ProgressBar)findViewById(R.id.pb_load);
        btn_setting = (Button)findViewById(R.id.btn_setting);
        btn_setting.setOnCreateContextMenuListener(this);
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.showContextMenu();
            }
        });
 
        tv_load.setVisibility(View.GONE);
        pb_load.setVisibility(View.GONE);
        btn_stargame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_load.setVisibility(View.VISIBLE);
                pb_load.setVisibility(View.VISIBLE);
                    timer.schedule(task,0,100);
 
 
            }
        });
    }
 
    Handler handler = new Handler(){
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x111:
                    pb_load.setProgress(progress);
                    tv_load.setText("正在加载");
                    break;
                case 0x222:
                    tv_load.setVisibility(View.GONE);
                    pb_load.setVisibility(View.GONE);
            }
        }
    };
 
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0,GAME_OPTION,1,"参数设置");
        menu.add(0,HERO_RECORD,2,"英雄排行");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu subMenu = menu.addSubMenu("设置");
        subMenu.add(0,GAME_OPTION,1,"参数设置");
        subMenu.add(0,HERO_RECORD,2,"英雄排行");
//        menu.add(0,GAME_OPTION,1,"参数设置");
//        menu.add(0,HERO_RECORD,2,"英雄排行");
        return super.onCreateOptionsMenu(menu);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case GAME_OPTION:
                break;
            case HERO_RECORD:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
 
 
}
 
(责任编辑:dnzg)