Android 对话框

在Android中,对话框是一个非常常见的控件,主要用于提示用户,对话框有许多不同的形式。

一、Dialog

Android官方对Dialog的解释:

A dialog is a small window that prompts the user to make a decision or enter additional information. 
A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

Dialog是一个在屏幕上弹出的对话框,用于用户做出选择或者输入额外的信息。一个对话框不会占满整个屏幕,并且通常需要用户做出决定才会继续执行。

Dialog是对话框的基类,我们应该使用它的子类来构建对话框:

java.lang.Object
   ↳     android.app.Dialog

Known Direct Subclasses
AlertDialog, CharacterPickerDialog, MediaRouteChooserDialog, MediaRouteControllerDialog, Presentation

Known Indirect Subclasses
DatePickerDialog, ProgressDialog, TimePickerDialog

二、AlterDialog

AlterDialog是Dialog的直接子类,它可以创建好几种不同的对话框,要使用AlterDialog创建一个对话框需要用到AlterDialog的一个内部类AlterDialog.Builder。使用AlertDialog,我们可以显示一个标题,最多3个按钮操作,以及一组选择框或者是自己定义的弹出框。

1.创建一个普通的对话框:

 

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("是否退出?");
builder.setIcon(R.mipmap.ic_launcher_round);//系统自带的圆形图片

//设置自定义标题,setCustomTitle会和setTitle和setIcon方法冲突
//当设置自定义标题之后setTitle和setIcon会失效
//        TextView textView = new TextView(this);
//        textView.setText("customTitle");
//        textView.setTextColor(Color.YELLOW);
//        textView.setBackgroundColor(Color.WHITE);
//
//        builder.setCustomTitle(textView);

//这是最右边的按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.No,Toast.LENGTH_LONG);
    }
});
//这是中间的按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.Yes,Toast.LENGTH_LONG);
    }
});
//这是左边的按钮
builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.Cancel,Toast.LENGTH_LONG);
    }
});
//显示对话框
builder.show();

上面是一个基础的对话框样式,可以将三个setXXXButton方法来设置按钮的数量。可以通过setCustomTitle自定义标题,自定义标题之后setTitle和setIcon都会失效。

2.创建一个单选对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
//builder.setMessage("是否退出?");
//builder.setIcon(R.mipmap.ic_launcher_round);

//自定义title
//    TextView textView = new TextView(this);
//    textView.setText("customTitle");
//    textView.setTextColor(Color.YELLOW);
//    textView.setBackgroundColor(Color.WHITE);
//    builder.setCustomTitle(textView);
//三个按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.No,Toast.LENGTH_LONG);
    }
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.Yes,Toast.LENGTH_LONG);
    }
});
builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,R.string.Cancel,Toast.LENGTH_LONG);
    }
});
//此方法指定对话框为单选框,并且此方法与setMessage方法冲突
//如果使用了setMessage会使此方法失效
builder.setSingleChoiceItems(new String[]{"选项1", "选项2", "选项3", "选项4"}, 0, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
});
builder.show();

上面的代码创建了一个单选框,注意此时不能使用setMessage,否则会使setSingleChoiceItems方法失效,单选对话框对话框会退化为普通的对话框,但是setTitle方法可以使用,不过此时需要把自定义标题去掉,否则无法显示。第一张图片是使用setTitle方法的结果,第二张是自定义Title的结果(比较丑...)。

2.多选对话框

builder.setMultiChoiceItems(new String[]{"选项1", "选项2", "选项3", "选项4"},
                new boolean[]{true, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });

上面代码创建一个多选对话框,注意和单选对话框相同,此时不能使用setMessage方法,并且如果在代码中同时使用了setSingleChoiceItem会产生很奇怪的现象,当然这两个不可能同时使用。

三、ProgressDialog

ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("加载中...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(200);
dialog.incrementProgressBy(20);//一级进度条,图片中颜色较深的那个
dialog.incrementSecondaryProgressBy(80);//二级进度条,图片中颜色较淡的那个
dialog.show();

progressDialog已经被Google作为过时的内容,不建议使用,所以没有大幅度讲解,progressDialog的替代品为progressBar。

更多的可以看看这个了解一下:https://blog.csdn.net/caesardadi/article/details/11982721

四、ProgressBar

还没来得及看......

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值