返回顶部

收藏

android中自己编写一个开始菜单并能设置墙纸的功能

更多

[Java]代码

list = new ArrayList<HashMap<String,String>>();
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("menuItemName", "文件");
        list.add(map);
        HashMap<String,String> map2 = new HashMap<String,String>();
        map2.put("menuItemName", "设置墙纸1");
        list.add(map2);
        HashMap<String,String> map3 = new HashMap<String,String>();
        map3.put("menuItemName", "设置墙纸2");
        list.add(map3);
        HashMap<String,String> map4 = new HashMap<String,String>();
        map4.put("menuItemName", "设置快捷1");
        list.add(map4);
        HashMap<String,String> map5 = new HashMap<String,String>();
        map5.put("menuItemName", "设置快捷2");
        list.add(map5);
        HashMap<String,String> map6 = new HashMap<String,String>();
        map6.put("menuItemName", "设置快捷3");
        list.add(map6);
        HashMap<String,String> map7 = new HashMap<String,String>();
        map7.put("menuItemName", "退出");
        list.add(map7);

        myButton = (Button)findViewById(R.id.myButton);

        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(state == 1) {
                    state = 0;
                    pop.dismiss();
                }
                else if(state == 0) {
                    // 弹出自定义的菜单
                    layout = getLayoutInflater().inflate(R.layout.menu_layout, null);
                    menulist = (ListView)layout.findViewById(R.id.menulist);
                    SimpleAdapter listAdapter = new SimpleAdapter(Testflag.this,list,R.layout.menu_item,new String[]{"menuItemName"},new int[]{R.id.menuitem});
                    menulist.setAdapter(listAdapter);

                    pop = new PopupWindow(layout, myButton.getWidth(),getWindowManager().getDefaultDisplay().getHeight()/3+40);

                    pop.update();
                    //pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    pop.setTouchable(true);
                    pop.setOutsideTouchable(true);
                    pop.setFocusable(true);
                    pop.showAtLocation(layout, (Gravity.BOTTOM-myButton.getHeight())|Gravity.LEFT, 0, 2*myButton.getHeight());
                    state = 1;
                    pop.setTouchInterceptor(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                pop.dismiss();
                                return true;
                            }
                            return false;
                        }

                    });
                    menulist.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            switch(arg2) {
                            case 0:
                                Toast.makeText(getApplicationContext(), "显示所有文件", Toast.LENGTH_SHORT).show();
                                pop.dismiss();
                                break;
                            case 1:
                                pop.dismiss();
                                Bitmap wall01 = getWallPaper01();
                                setWallPaper(wall01);
                                break;
                            case 2:
                                pop.dismiss();
                                Bitmap wall02 = getWallPaper02();
                                setWallPaper(wall02);
                                break;
                            case 3:
                                Toast.makeText(getApplicationContext(), "设置快捷1", Toast.LENGTH_SHORT).show();
                                pop.dismiss();
                                break;
                            case 4:
                                Toast.makeText(getApplicationContext(), "设置快捷2", Toast.LENGTH_SHORT).show();
                                pop.dismiss();
                                break;
                            case 5:
                                Toast.makeText(getApplicationContext(), "设置快捷3", Toast.LENGTH_SHORT).show();
                                pop.dismiss();
                                break;
                            case 6:
                                Toast.makeText(getApplicationContext(), "退出", Toast.LENGTH_SHORT).show();
                                pop.dismiss();
                                break;
                            }
                        }

                    });
                }
            }

        });

标签:Android,java

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 尖兵 发表 2018-07-28 04:31:07 Android Studio之Debug(二):自定义Debug变量视图
  2. hellas 发表 2018-09-12 14:18:39 Android 构建那些事
  3. xiaoli.wang 发表 2018-09-16 13:40:57 rxjava2.x源码学习随笔
  4. K-Res 发表 2015-02-27 09:00:16 Android中ObjectAnimator为Activity、Fragment设置过渡动画效果的方
  5. 博主 发表 2015-02-25 15:49:38 Mac安装Android开发环境
  6. chen 发表 2014-09-23 10:45:44 安卓备忘八则
  7. niyingxunzong 发表 2015-10-30 07:01:36 Gson 使用简介(Gson User Guide 一)
  8. 博主 发表 2017-10-22 06:21:10 Mac安装Android开发环境
  9. shangxuetang 发表 2014-12-22 04:52:10 Android开发,WebView详解
  10. hanze 发表 2018-05-20 11:05:43 随想录(对比着c学java)
  11. 雨点点 发表 2016-05-12 04:59:29 Android中Java和JavaScript交互
  12. CppLive 发表 2013-06-24 14:52:04 MAC跟Linux下Android交叉编译环境搭建

发表评论