Android中AlertDialog(对话框)的使用

来源:互联网 发布:贝莱隆人品 知乎 编辑:程序博客网 时间:2024/04/28 09:11

本人菜鸟一枚,初写博客,不喜勿喷。

一,普通的确定对话框

这里写图片描述

该效果是按下返回键,确保不误操作。

代码如下:

public void dialog(){//该方法是创建对话框的方法    new AlertDialog.Builder(this)//使用的建造者模式        .setTitle("提示")        .setMessage("确认退出这个Activity吗?")        .setIcon(R.mipmap.ic_launcher)//设置标题图标        .setPositiveButton("取消",null)        .setNegativeButton("确定", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {//确定按钮的点击事件            finish();//finish当前这个Activity。            }        })        .show();    }

二,单选的对话框

只能选择一项

这里写图片描述

代码如下

public void dialog(){    final String s[]=new String[]{"呵呵呵", "哈哈哈", "嘿嘿嘿"};//要显示的数组    new AlertDialog.Builder(this)//创建AlertDialog的对象            .setIcon(R.mipmap.ic_launcher)//设置标题图标            .setTitle("单选框")            .setSingleChoiceItems(//设置选项为单选            s,//选项的内容(String数组中的)            1,//初始选中的项(初始选中为第一项“哈哈哈”,-1为都不选中)            new DialogInterface.OnClickListener() {//选中监听                    @Override                    public void onClick(DialogInterface dialog, int which) {                        num=which;//这里是选中的项,num是全局变量                    }                })            .setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮的监听                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(AlertDialogActivity.this, s[num]+"", Toast.LENGTH_SHORT).show();//Toast打印选中的String数组中的项                    }                })            .show();    }

三,多选对话框

可以多选
这里写图片描述

代码如下:

    public void dialog(){        final boolean selected[]=new boolean[]{false,false,false};        new AlertDialog.Builder(this)//创建对话框                .setIcon(R.mipmap.ic_launcher)//标题图标                .setTitle("多选框")                .setMultiChoiceItems(new String[]{"大老爷们", "抠脚女汉子", "洪兴十三妹"},//显示数据                        selected,//初始选中对应的boolean类型数组。                        new DialogInterface.OnMultiChoiceClickListener() {                            @Override                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {//点击回调的监听                                selected[which]=isChecked;                            }                        })                .setNegativeButton("确定", new DialogInterface.OnClickListener() {//确定键的监听                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Log.i("yyy",selected[0]+""+selected[1]+selected[2]+"hehehehe");                        //选中的结果在boolean类型数组中(下标与数据源相对应)储存,true为选中,false为没选中。                    }                })                .setPositiveButton("取消",null)                .show();    }

四,列表框

只能选择一项
这里写图片描述

代码如下:

    public void dialog(){        final String s[]=new String[]{"小", "中", "大"};//数据源                new AlertDialog.Builder(this)//创建对象                .setIcon(R.mipmap.ic_launcher)//标题图标                .setTitle("列表框")                .setItems(s,//设置数据源                        new DialogInterface.OnClickListener() {//设置监听                    @Override                    public void onClick(DialogInterface dialog, int which) {//which是所选的数据源的项                        Toast.makeText(AlertDialogActivity.this,s[which], Toast.LENGTH_SHORT).show();                    }                })                .show();    }

五,三个按钮的对话框

这里写图片描述

这种三个按钮对于有些机型来说不是很美观(不对称)比如以下这样
这里写图片描述

(如果需要的话)个人觉得使用自定义对话框的比较好!

代码如下:

    public void dialog(){            new AlertDialog.Builder(this)//标题栏对象                .setIcon(R.mipmap.ic_launcher)                .setTitle("调查")                .setMessage("对本开发者长相的评价")                .setPositiveButton("可爱", new DialogInterface.OnClickListener() {//监听“可爱”按钮                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(AlertDialogActivity.this, "是个乖乖娃儿!", Toast.LENGTH_SHORT).show();                    }                })                .setNegativeButton("很帅", new DialogInterface.OnClickListener() {//监听“很帅”按钮                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(AlertDialogActivity.this, "帅呆了", Toast.LENGTH_SHORT).show();                    }                })                .setNeutralButton("超级帅", new DialogInterface.OnClickListener() {//监听“超级帅”按钮                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(AlertDialogActivity.this, "宇宙第一帅", Toast.LENGTH_SHORT).show();                    }                })                .show();    }

六,自定义对话框

界面比较丑,不要在意细节!

这里写图片描述

代码如下:

    public void but7(){        View view = LayoutInflater.from(this).inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.asd));//把xml解析成View        final EditText ed= (EditText) view.findViewById(R.id.edit);        new AlertDialog.Builder(this)                .setIcon(R.mipmap.ic_launcher)                .setTitle("自定义")                .setView(view)//设置自定义的View                .setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定监听                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(AlertDialogActivity.this,ed.getText(), Toast.LENGTH_SHORT).show();                    }                })                .setNegativeButton("取消",null)                .show();    }

xml如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:id="@+id/asd"    android:layout_marginLeft="20dp"    android:layout_marginRight="20dp"    android:layout_width="match_parent"    android:layout_height="match_parent"><TextView    android:layout_weight="0"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="请输入"    />    <EditText        android:id="@+id/edit"        android:layout_weight="1"        android:layout_marginLeft="20dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入"        /></LinearLayout>

哪里有错的地方也希望网友们多多指出 !!!

1 0