欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android之Dialog详解

程序员文章站 2024-02-10 22:11:28
...

在实际开发中,用系统的对话框会很少,因为太丑了,美工不愿意,多是使用自定义对话框。当然学会系统的,自定义就简单了,所以我们先来学习系统的,后面在写一篇自定义对话框。接下来就看看有哪些对话框吧:

        一、普通对话框:

        Android之Dialog详解

代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("危险应用");
builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        showToast("您点击的是删除按钮!");

    }
});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        showToast("您点击的是取消按钮!");
    }
});
AlertDialog alertDialog = builder.create();
alertDialog.show();

//关闭对话框
alertDialog.dismiss();

  

二、列表对话框:

Android之Dialog详解

代码:

final String[] items = {"小丽","小红","小芳"};
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("您选择的课程:");
builder2.setItems(items, new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface arg0, int arg1) {
 
     }
});
 
builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {

    @Override
     public void onClick(DialogInterface arg0, int arg1) {
 
     }
});
AlertDialog items_dialog = builder2.create();
items_dialog.show();

  

三、单选对话框:

Android之Dialog详解

代码:

 

final String[] items = {"小丽","小红","小芳"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setTitle("请选择您的课程:");
builder3.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int which) {
        checkedItem = which;
    }
});

builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        showToast("您选择的是:" + items[checkedItem]);
    }
});

builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {

    }
});
AlertDialog single_dialog = builder3.create();
single_dialog.show();

 

四、多选对话框:

Android之Dialog详解

代码:

 

final String[] items = {"小丽","小红","小芳"};
AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
builder4.setTitle("请选择您的课程:");
builder4.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int which, boolean isChecked) {
        // 当用户点击复选框时执行的操作
        // 将位置与状态进行绑定
        // showToast(which + "****************" +isChecked);
        checkedItems[which] = isChecked;
    }
});

builder4.setPositiveButton("确定", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < checkedItems.length; i++) {
            if (checkedItems[i]) {
                sb.append("第" + i + "个" + items[i] + "选项的状态是:" + checkedItems[i] + "\r\n");
            }

        }
        showToast(sb.toString());
    }
});

builder4.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {

    }
});
AlertDialog multi_dialog = builder4.create();
multi_dialog.show();

 

五、日期对话框:

Android之Dialog详解

代码:

 

// 通过Calendar类来获取系统的日期
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int monthOfYear = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

// 日期对话框

DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // 监听用户时间的改变
        showToast("您选择的日期是:" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");

    }// 当用户点击日期对话框的日期,对话框的日期根据系统的日期而变化
}, year, monthOfYear, dayOfMonth);

dateDialog.show();

 

六:时间对话框:

Android之Dialog详解

代码:

Calendar calendar2 = Calendar.getInstance();
int hourOfDay = calendar2.get(Calendar.HOUR);
int minute = calendar2.get(Calendar.MINUTE);

// 时间对话框
TimePickerDialog timeDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        showToast("您选择的时间是" + hourOfDay + ":" + minute);
    }
}, hourOfDay, minute, true);

timeDialog.show();

  

七:进度条对话框:

Android之Dialog详解

代码:

 

final ProgressDialog progressDialog = new ProgressDialog(this);
// 将进度条设置成水平
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置最大值
progressDialog.setMax(100);

progressDialog.show();

// 线程
new Thread(new Runnable() {

    @Override
    public void run() {
        while (true) {
            progressDialog.setProgress(progress);
            progress++;

            SystemClock.sleep(new Random().nextInt(100));

            if (progress == 100) {
                progressDialog.dismiss();
            }
        }
    }
}).start();

 

进度对话框:

public void dialog04(View v){

        //方法1: 很多 地方 , 干了 比较耗时的事儿 时 会使用到的 
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage("正在拼命加载中....");
        dialog.show();
//        
//        try {
//            Thread.sleep(3000);
//            dialog.dismiss();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }


        //方法2: 这行 代码 可以 让其 显示, 查看源代码 , 实际上是调用了  上面的这些代码 
        //ProgressDialog.show(this, "消息", "正在拼命加载中....").show();


        //关闭隐藏对话框
        dialog.dismiss();

}

  

 

八:自定义对话框:

Android之Dialog详解

代码:

View view = View.inflate(this, R.layout.login_layout, null);

// 通过view去找到这些控件
pwd_first = (EditText) view.findViewById(R.id.pwd_first);
pwd_confirm = (EditText) view.findViewById(R.id.pwd_confirm);
cancle = (Button) view.findViewById(R.id.cancle);
confirm = (Button) view.findViewById(R.id.confirm);

cancle.setOnClickListener(this);
confirm.setOnClickListener(this);

customDialog = new AlertDialog.Builder(this).setTitle("登录").setView(view).create();

customDialog.show();