zoukankan      html  css  js  c++  java
  • alertdialog简单用法

    new AlertDialog.Builder(MainActivity.this)
                    .setTitle("aaa")
                    .setIcon(R.drawable.ic_launcher)
                    .setMessage("next")
                    .setPositiveButton("确定按钮",
                            new DialogInterface.OnClickListener() {
    
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
    
                                }
                            }).setNegativeButton("普通按钮", new DialogInterface.OnClickListener() {
                                
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    
                                }
                            }).setNeutralButton("取消按钮", new DialogInterface.OnClickListener() {
                                
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    
                                }
                            }).show();

     列表项的对话框

    private void itemDialog(){
            final String[] levle = {"1","2","3"};
            new AlertDialog.Builder(MainActivity.this)
            .setTitle("选择级别")
            //添加取消按钮,没有写事件
            .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    
                }
            })
            //添加列表项  参数分别为数据,事件
            .setItems(levle, new DialogInterface.OnClickListener() {
                
                @Override
                //点击列表项会把结果显示到tv里
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.this.tv.setText("选择的级别是"+levle[which]);
                }
            }).show();
        }

     复选对话框

    private void muliItem(){
            final String[] data ={"1","2","3"};
            boolean[] b = {true,false,false};
            new AlertDialog.Builder(MainActivity.this)
            .setTitle("ok")
            //传入的参数分别是数据,默认选中状态,事件
         .setMultiChoiceItems(data, b,
    new OnMultiChoiceClickListener() { @Override         
    public void onClick(DialogInterface dialog, int which, boolean isChecked) { String msg =""; for (int i = 0; i < data.length; i++) { if(i==which && isChecked){ msg += data[which]; } } MainActivity.this.tv.append(msg); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); }

     用反射技术防止对话框意外关闭

    //防止对话框意外关闭
        private void setCloseable(DialogInterface dialog,boolean b) {
            // TODO Auto-generated method stub
            Field field ;
            try {
                field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                field.setAccessible(true);
                field.set(dialog,b);
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    调用的时候直接调用就可以,传入的值为false就是不能关,true就是可以关闭

  • 相关阅读:
    spingboot集成jpa(二)
    datanode与namenode的通信
    Yarn NodeManager restart
    hadoop上线和下线节点
    Spark读取文件
    eclipse安装中文补丁包
    eclipse中maven打包
    (转) eclipse安装lombok
    Windows 访问 Oracle
    wCF 问题收集页
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4884887.html
Copyright © 2011-2022 走看看