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就是可以关闭

posted @ 2015-10-16 11:47  aaddrrooiidd  阅读(172)  评论(0编辑  收藏  举报