码迷,mamicode.com
首页 > 其他好文 > 详细

Dialog对话框

时间:2017-03-10 22:27:19      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:样式   功能   idt   als   which   ems   tin   offset   返回   

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对话框

标签:样式   功能   idt   als   which   ems   tin   offset   返回   

原文地址:http://www.cnblogs.com/xiaolai1995/p/6533118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!