Android基础 学习之Dialog

来源:互联网 发布:域名备案号 编辑:程序博客网 时间:2024/05/16 06:02

1.警告对话框AlertDialog
这个比较简单,直接上代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setMessage("你是傻逼?")//设置提示信息                .setPositiveButton("是的", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.cancel();//确定事件在此处理                    }                })                .setNegativeButton("不是", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "傻逼!",                                Toast.LENGTH_LONG).show();//取消事件在此处理                    }                });        builder.setIcon(R.drawable.question);///设置提示图片        builder.setTitle("提示");//设置提示标题        AlertDialog alertDialog = builder.create();//创建Dialog使用Builder数据        alertDialog.show();//show出来

2.进度条对话框ProgressDialog
创建进度条对话框:

ProgressDialog progressDialog = new ProgressDialog(this);

设置各种参数:

progressDialog.setTitle("加载数据");progressDialog.setMessage("正在加载数据中...");        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置style,此处使用长条进度条progressDialog.setMax(100);progressDialog.show();//show,不然无法看到的progressDialog.setProgress(0);//设置初始值

长条进度条不会自动更新视图,所以使用线程更新视图,才会有动态效果:

new Thread() {            public void run() {                try {                    int cnt = 0;                    while (true) {                        Thread.sleep(100);                        cnt += 1;//刷新一次加1                        progressDialog.setProgress(cnt);                        if (cnt == 100)//当进度条满,则退出                            break;                    }                    progressDialog.dismiss();//销毁对话框                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }.start();

3.自定义对话框
必须重写onCreateDialog方法,然后声明初始化一个Dialog

@Override@Deprecatedprotected Dialog onCreateDialog(int id) {Dialog dialog = new Dialog(this);}

声明一个key常量,如:MYDIALOG,用于区分是哪个Dialog,如果多个自定义对话框同时存在时。

3.1.添加自定义对话框视图

这里写图片描述

3.2.Java代码部分,在onCreateDialog方法中对对话框的各种处理

if (id == MYDIALOG) {//用到前面定义的key            dialog.setContentView(R.layout.dialogview);//将自定义视图绑定            dialog.setTitle("自定义对话框");            //取出视图中的控件            final EditText et_Name = (EditText) dialog                    .findViewById(R.id.et_Name);            final EditText et_Gender = (EditText) dialog                    .findViewById(R.id.et_Gender);            Button btn_ok = (Button) dialog.findViewById(R.id.btn_ok);            Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);            btn_ok.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {//自定义控件事件处理                    MainActivity.this.tv_msg.setText(et_Name.getText()                            .toString() + "" + et_Gender.getText().toString());                    dialog.dismiss(); //销毁对话框                }            });            btn_cancel.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    dialog.dismiss();                }            });        }        return dialog;//将自定义对话框返回

最后不要忘了把自定义对话框显示出来(showDialog方法会自动寻找onCreateDialog方法,所以需要重写该方法):

this.showDialog(MYDIALOG);//在当前Activity中show出来
0 0