Andriod从入门到熟悉之 Dialog

公共部分
private AlertDialog alert = null;
private AlertDialog.Builder builder = null;
alert = null;
builder = new AlertDialog.Builder(MainAdpterActivity.this);
//含三个按钮的Dialog
                alert = builder.setIcon(R.mipmap.group4)
                        .setTitle("股票大跌怎么办")
                        .setMessage("请参考拔网线立即止跌法")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainAdpterActivity.this,"点击了取消",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainAdpterActivity.this,"确定",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("其他", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainAdpterActivity.this,"其他",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alert.show();
//普通列表Dialog
final String[] Person = new  String[]{"恩来","少奇","泽东","德怀","小雅","丹阳"};
alert = builder.setIcon(R.mipmap.group4)
        .setTitle("选择你喜欢的人")
        .setItems(Person, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainAdpterActivity.this,"你选择了"+Person[which],Toast.LENGTH_SHORT).show();
            }
        })
        .create();
alert.show();
//单选列表对话框
                alert = builder.setIcon(R.mipmap.group4)
                        .setTitle("选择你喜欢的人,智能选择一个")
                        .setSingleChoiceItems(Person, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainAdpterActivity.this,"你选择了"+Person[which],Toast.LENGTH_SHORT).show();

                            }
                        })
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                alert.dismiss();
                            }
                        })
                        .create();
                alert.show();
final String[] Person = new  String[]{"恩来","少奇","泽东","德怀","小雅"};
//记录列表状态数组
personLike = new  boolean[]{false,false,false,false,false};


//多选列表对话框
alert = builder.setIcon(R.mipmap.group4)
        .setTitle("选择你喜欢的人,智能选择一个")
        .setMultiChoiceItems(Person, personLike, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                personLike[which] = isChecked;
            }
        })
        .setNegativeButton("取消",null)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String reslut = "";
                for (int i =0;i<personLike.length;i++){
                    if (personLike[i]){
                        reslut += Person[i]+" ";
                    }
                }
                Toast.makeText(MainAdpterActivity.this,reslut,Toast.LENGTH_SHORT).show();
            }
        })
        .create();


alert.show();

//自定义View Build.setView()

final LayoutInflater inflater = MainAdpterActivity.this.getLayoutInflater();
dialogView = inflater.inflate(R.layout.view_dialog_layout,null);


builder.setView(dialogView);
alert = builder.create();
alert.show();


dialogView.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alert.dismiss();
    }
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值