欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Andriod从入门到熟悉之 Dialog

程序员文章站 2022-03-10 15:47:22
...
公共部分
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();
    }
});

 

相关标签: Andriod