码迷,mamicode.com
首页 > 其他好文 > 详细

PopupMenu和对话框的使用

时间:2016-08-01 10:34:39      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

PopupMenu和对话框的使用

1.菜单的使用之 PopupMenu

 

 

步骤:1.创建popupMenu对象 参数:上下文对象   当前view对象

            2.利用getMenu().addSubMenu创建子菜单

               3..show();显示菜单

   @SuppressLint("NewApi")
    public void popclick(final View view){
        PopupMenu pop=new PopupMenu(this,view);
        //添加一级菜单
        SubMenu sub=pop.getMenu().addSubMenu("文字颜色");
        //添加二级caidan 
        sub.add("红色");
        sub.add("绿色");
        sub.add("蓝色");
        //添加一级菜单
        SubMenu sub2=pop.getMenu().addSubMenu("文字大小");
        //添加二级caidan 

       //中间 1 2 3为 id
        sub2.add(0,1,0, "大");
        sub2.add(0,2,0,"中");
        sub2.add(0,3,0,"小");
        //显示出来
        pop.show();
        //监听事件
        pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //设置按钮文字的大小和颜色
                Button btn=(Button)view;
                if(item.getTitle().equals("红色")){
                    btn.setTextColor(Color.RED);
                }else if(item.getTitle().equals("绿色")){
                    btn.setTextColor(Color.GREEN);
                }else if(item.getTitle().equals("蓝色")){
                    btn.setTextColor(Color.BLUE);
                }
                
                switch (item.getItemId()) {
                case 1:
                    btn.setTextSize(25);
                    break;
                case 2:
                    btn.setTextSize(15);
                    break;
                case 3:
                    btn.setTextSize(10);
                    break;
                }
                return false;
            }
        });
        
    }

 

2.对话框

 

 

1.PopupWindow 的使用(需要加载自定义布局文件)

    public void doClick(View view){
        //把xml转换成view
        View popView = LayoutInflater.from(this).inflate(R.layout.popupwindow, null);
        //实例化popupwindow  参数 view 宽  高
        PopupWindow pop=new PopupWindow(popView, 460, 460);
        //设置背景 是触摸空白处消失
        pop.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //设置空白处可以触摸
        pop.setOutsideTouchable(true);
        //显示
        pop.showAtLocation(view, Gravity.CENTER, 0, 0);
        //找到弹出对话框布局的点击View对象
        Button btn=(Button) popView.findViewById(R.id.btn_pop);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                  Random rd=new Random();
                  float money=rd.nextFloat()*100;
                  //保留两位小数点
                  DecimalFormat df=new DecimalFormat("#.00");
                Toast.makeText(MainActivity.this,"您抢到了"+df.format(money)+"元", 0).show();

                //对话框消失
                pop.dismiss();
            }
        });
        
    }

 

2. AlertDialog的使用

public void doclick(View view){
        //创建AlertDialog.builder对象
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //设置标题和点击事件
        builder.setTitle("温馨提示:").setMessage("你敢点击我吗?").setPositiveButton("确定",new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this,"您真的点击了我",0).show();
                finish();    
            }
        }).setNegativeButton("取消", new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                        
            }
        });
        
        Dialog dia=builder.create();
        dia.show();
    }

3.ProgressDialog进度条


    public void doclick(View view){
        //创建ProgressDialog对象
         ProgressDialog dialog=new ProgressDialog(this);
         //设置标题
         dialog.setTitle("下载提示");
        //设置提示文本
         dialog.setMessage("正在下载...");
         //设置风格
         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         //设置不确定值 true不确定  false 确定
         dialog.setIndeterminate(false);
         //设置不可以取消
         dialog.setCancelable(true);
         //设置最大值
         dialog.setMax(100);
         dialog.setProgress(88);
         //显示
         dialog.show();
    }

PopupMenu和对话框的使用

标签:

原文地址:http://www.cnblogs.com/android-blogs/p/5724739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!