博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dialog对话框
阅读量:5364 次
发布时间:2019-06-15

本文共 7468 字,大约阅读时间需要 24 分钟。

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();                    }                });            }        });

 

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

 

转载于:https://www.cnblogs.com/xiaolai1995/p/6533118.html

你可能感兴趣的文章
HBase详解
查看>>
HDFS(Hadoop Distributed File System)的组件架构概述
查看>>
MetalLB自建私有Kubernetes的LoadBalancer负载均衡类型服务
查看>>
HBase的应用场景及特点
查看>>
kubernetes之有状态应用StatefulSet
查看>>
kubernetes(k8s)Pod污点与容忍
查看>>
kubernetes Pod的升级与回滚
查看>>
Python 爬取12306火车票
查看>>
linux源码安装python及pip和django
查看>>
Prometheus监控node-exporter常用指标含义
查看>>
源码安装zabbix
查看>>
Python 空值和非空值
查看>>
HAProxy服务器 、Keepalived热备 、Keepalived+LVS
查看>>
Python 让输入的密码不在屏幕上显示
查看>>
Python 让文件代码支持汉字
查看>>
Go 程序开发的注意事项
查看>>
Python 循环语句
查看>>
python pillow模块用法
查看>>
Python pillow库安装报错
查看>>
tesserocr与pytesseract模块的使用
查看>>