Dialog 控件的使用

1、点击按钮弹出框下有两个按钮的例子,如图:
两个按钮提示

// 写一个方法放到按钮下,并设置点击监听
private void show2ButtonDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 组装弹出框的内容
        builder.setTitle("删除警告");
        builder.setIcon(R.drawable.blue_button);
        builder.setMessage("是否删除该文件");
        // 配置正向按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 处理点击确定之后的事情
                Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // 反向按钮
        builder.setNegativeButton("取消", null);
        // 创建弹出框对象
        AlertDialog d = builder.create();
        // 让返回键不能隐藏弹出框
        d.setCancelable(false);
        // 显示弹出框
        d.show();
    }

2、三个按钮的例子,如图:
三个按钮的弹出框

private void show3ButtonDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 组装弹出框的内容
        builder.setTitle("删除警告");
        builder.setIcon(R.drawable.blue_button);
        builder.setMessage("请为该服务打分");
        // 配置正向按钮
        builder.setPositiveButton("满意", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 处理点击确定之后的事情
                Toast.makeText(MainActivity.this, "点击了满意", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // 设置中立
        builder.setNeutralButton("一般", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了一般", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // 反向按钮
        builder.setNegativeButton("不满意", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了不满意", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        // 创建弹出框对象
        AlertDialog d = builder.create();
        // 让返回键不能隐藏弹出框
        d.setCancelable(false);
        // 显示弹出框
        d.show();
    }

3、有输入框的弹出框,如图:
输入框的弹出框

1、先写一个dialog_view.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/et_psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码" />

</LinearLayout>
2、 写一个方法

private void showViewDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 加载视图
        View layout = getLayoutInflater().inflate(R.layout.dialog_view, null);
        // 局部的内部类要访问局部变量,那么该局部变量必须是final类型
        final EditText etName = (EditText) layout.findViewById(R.id.et_name);
        final EditText etPsw = (EditText) layout.findViewById(R.id.et_psw);
        // final EditText etFileName = new EditText(this);
        // 设置显示的布局
        builder.setView(layout);
        // 设置标题
        builder.setTitle("用户登录");
        // 配置正向按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = etName.getText().toString();
                String psw = etPsw.getText().toString();
                // 处理点击确定之后的事情
                Toast.makeText(MainActivity.this, name + " | " + psw,
                        Toast.LENGTH_SHORT).show();
            }
        });
        // 反向按钮
        builder.setNegativeButton("取消", null);
        // 创建弹出框对象
        AlertDialog d = builder.create();
        // 让返回键不能隐藏弹出框
        d.setCancelable(false);
        // 显示弹出框
        d.show();
    }

4、有列表的弹出框,如图:
列表的弹出框

private void showListDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 列表显示的内容
        // String[] items = { "复制", "剪切", "重命名", "删除", "发送到" };
        builder.setTitle("请选择操作");
        // 配置显示列表
        builder.setItems(R.array.opts, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了" + which,
                        Toast.LENGTH_SHORT).show();
            }
        });
        // 反向按钮
        builder.setNegativeButton("取消", null);
        // 创建弹出框对象
        AlertDialog d = builder.create();
        // 让返回键不能隐藏弹出框
        d.show();
    }

5、单选列表的弹出框
单选列表的弹出框

private void showSigleChiose() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 设置单选列表
        builder.setSingleChoiceItems(R.array.sigle_chiose, 0,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        // 创建弹出框对象
        AlertDialog d = builder.create();
        d.show();
    }

6、多选列表的弹出框
多选列表的弹出框

private void showMulChiose() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 设置单选列表
        builder.setMultiChoiceItems(R.array.like, new boolean[] { true, false,
                true, false, false },
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        Toast.makeText(MainActivity.this,
                                which + " | " + isChecked, Toast.LENGTH_SHORT)
                                .show();
                    }
                });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        // 创建弹出框对象
        AlertDialog d = builder.create();
        d.show();
    }

7、正在加载的弹出框
加载框

ProgressDialog progressDialog;
private void showProgressDialog() {
        if (null == progressDialog) {
            progressDialog = ProgressDialog.show(this, null, "正在加载中...");
            progressDialog.setCancelable(true);
        } else {
            progressDialog.show();
        }
    }

8、PopupWindow 弹出框的使用
PopupWindow

1、新建一个window_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff999999"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/item_0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff333333"
        android:gravity="center"
        android:padding="5dp"
        android:text="大文件"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/item_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff333333"
        android:gravity="center"
        android:padding="5dp"
        android:text="搜索"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/item_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff333333"
        android:gravity="center"
        android:padding="5dp"
        android:text="设置"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/item_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff333333"
        android:gravity="center"
        android:padding="5dp"
        android:text="反馈"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/item_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff333333"
        android:gravity="center"
        android:padding="5dp"
        android:text="退出"
        android:textColor="#ffffff"
        android:textSize="20sp" />

</LinearLayout>
2、在java中PopupWindow的使用
private PopupWindow window;
private void showWindow() {
        if (null == window) {
            View layout = getLayoutInflater().inflate(R.layout.window_layout,
                    null);
            // 初始化菜单的点击监听
            layout.findViewById(R.id.item_0).setOnClickListener(this);
            // 创建PopuWindow显示菜单
            window = new PopupWindow(layout, 300,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            // 配置背景资源(才能够响应返回键)
            window.setBackgroundDrawable(new ColorDrawable());
            window.setOutsideTouchable(true);
            window.setFocusable(true);
        }
        // 显示(参考的某个视图对象,x方向的偏移,y方向的偏移)
        // window.showAsDropDown(btn8, 100, 0);
        // 显示在一个容器中(容器对象,显示的相对位置,基于相对位置的x偏移,基于相对位置的y偏移)
        window.showAtLocation(mainLayout, Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
    }
以上就是Dialog的常用方法
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值