Dialog对话框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dialog对话框相关的知识,希望对你有一定的参考价值。

Dialog对话框

这个内容比较简单,所以就没用很多的文字解释,直接看代码

这是我们要讲的展示的所有东西:

技术分享

首先我们来展示一些吐丝:有上诉4种:(这里的代码省略了基本的内容,只显示具体功能的代码)

//吐丝
        bt1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Toast toast = Toast.makeText(MainActivity.this, "吐丝", Toast.LENGTH_SHORT);                    
                 toast.show(); 
            }
        });    

技术分享

//居中吐丝
        bt2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "吐丝", Toast.LENGTH_SHORT);
                /**
                 * toast.setGravity(int gravity, int xOffset, int yOffset)
                 * gravity:初始位置
                 * xOffset X轴的偏移量,+向右,-向左
                 * yOffset Y轴的偏移量,+向下,-向上
                 */
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });

技术分享

//图片吐丝
        bt3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "吐丝", Toast.LENGTH_SHORT);
                LinearLayout toast_layout = (LinearLayout)toast.getView();//得到toast的view(里面有吐丝两个字)
                ImageView iv = new ImageView(MainActivity.this);
                iv.setImageResource(R.drawable.ic_launcher);
                toast_layout.addView(iv);//往toast的view里面添加一个图片,因为toast是linerLayout类型,所以加在了下面
                toast.show();
            }
        });

 

技术分享

 //自定义吐丝
        bt4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast toast = new Toast(MainActivity.this);
                //将布局变成view对象
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View view = inflater.inflate(R.layout.diy, null);
                toast.setView(view);//调用toast的setView方法就能将view替换toast的样子
                toast.show();
            }
        });

技术分享

接下来我们来展示一些对话框:

总的来说对话框是构建一个AlertDialog.Builder对象

这个对象是用来创建对话框的,通过把标题,图片,样式都传入给AlertDialog.Builder对象,

然后通过AlertDialog.Builder对象的create()方法返回一个AlertDialog对象,最后展示在页面上

 

AlertDialog.Builder对象的具体方法:

技术分享

Bt1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("确认对话框");
                builder.setIcon(R.drawable.ic_launcher);
                builder.setMessage("对话框显示的内容");
                builder.setPositiveButton("取消", new android.content.DialogInterface.OnClickListener() {                
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "取消被点拉", Toast.LENGTH_SHORT);                    
                         toast.show(); 
                    }
                });
                builder.setNegativeButton("确认", new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, "确认被点拉", Toast.LENGTH_SHORT);                    
                         toast.show(); 
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

技术分享

Bt2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("单选对话框");
                builder.setIcon(R.drawable.ic_launcher);
                /**
                 * builder.setSingleChoiceItems(String[] items, int checkedItem, listener);
                 * items:单选框的选项        这一参数除了可以是字符串,还可以是游标,适配器内的东西
                 * chekedItem:默认被选中的选项
                 */
                builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
                    //int which:被选中的
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, s[which]+"被选拉", Toast.LENGTH_SHORT);                    
                         toast.show(); 
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

技术分享

Bt3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("多选对话框");
                builder.setIcon(R.drawable.ic_launcher);
                /**
                 * builder.setMultiChoiceItems(String items, boolean[] checkedItems, listener)
                 * items:多选框的选项    这一参数除了可以是字符串,还可以是游标,适配器内的东西
                 * chekedItem:是否被选中
                 */
                builder.setMultiChoiceItems(s2, null, new OnMultiChoiceClickListener() {
                    //int which:被选中的         isChecked:true(没选的被选了)false(选了又取消了)
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if(isChecked){
                            Toast toast = Toast.makeText(MainActivity.this, s2[which]+"被选拉", Toast.LENGTH_SHORT);                    
                             toast.show(); 
                        }else{
                            Toast toast = Toast.makeText(MainActivity.this, s2[which]+"没被选拉", Toast.LENGTH_SHORT);                    
                             toast.show(); 
                        }    
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();;
            }
        });

技术分享

Bt4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("列表对话框");
                builder.setIcon(R.drawable.ic_launcher);
                builder.setItems(s2, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(MainActivity.this, s2[which]+"被点拉", Toast.LENGTH_SHORT);                    
                         toast.show();     
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

技术分享

Bt5.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("自定义对话框");
                builder.setIcon(R.drawable.ic_launcher);
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View view = inflater.inflate(R.layout.diy, null);
                builder.setView(view);
                AlertDialog dialog = builder.create();
                dialog.show();
                bt = (Button)view.findViewById(R.id.bb);
                bt.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Toast toast = Toast.makeText(MainActivity.this, "被发送了", Toast.LENGTH_SHORT);                    
                         toast.show();
                    }
                });
            }
        });

技术分享

 

如果有什么错误,或者我理解错误或不当的,恳请大家纠正,谢谢!嘻嘻嘻

 

以上是关于Dialog对话框的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法从我的 Dialog 片段中的 Activity 调用方法?

日期选择器对话框在片段中不起作用[关闭]

对话框片段包始终为空

如何在对话框中用容器视图替换片段

PyQt5对话框窗口打开时没有布局

UI5 - XML 片段中的格式化程序只执行一次。如何再次运行它们?