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.DialogKnown Direct SubclassesAlertDialog, CharacterPickerDialog, MediaRouteChooserDialog, MediaRouteControllerDialog, PresentationKnown Indirect SubclassesDatePickerDialog, 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
还没来得及看......
更多相关文章
- Android旋转屏幕不销毁数据的方法
- Android程序设置成横屏方法
- android 开发中将十六进制 颜色代码 转换为int类型数值 方法 :
- 详解Android读取本地图片和网络图片的方法
- android onSaveInstanceState的使用方法
- Android之复选框对话框
- Android之列表对话框
- android获取sd卡路径方法:
- Android之普通对话框