• PopupMenu和对话框的使用


    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();
        }

  • 相关阅读:
    R语言ENTREZID ID 转换 gene SYMBOL
    annvar下载数据库的网址
    R语言实现定性资料的秩和检验
    如何获得FPKM/RPKM计算需要的基因长度(考虑exon之间的overlap)
    linux和windows中文显示乱码问题
    从一篇nature medicine文章的得到的可用于文献画图的19种16进制颜色码
    简单将ggplot多个图排列在一起
    python通用读取vcf文件的类(可以直接复制粘贴使用)
    ROCR包中ROC曲线计算是取大于cutoff还是大于等于cutoff
    tinyproxy 反向代理无法上网原因
  • 原文地址:https://www.cnblogs.com/android-blogs/p/5724739.html
Copyright © 2020-2023  润新知