当前位置: 代码迷 >> 综合 >> Android常用对话框大全——AlertDialog
  详细解决方案

Android常用对话框大全——AlertDialog

热度:60   发布时间:2023-11-19 12:21:03.0

Android常用对话框大全——AlertDialog

  • 普通的对话框
AlertDialog dialog = new AlertDialog.Builder(this).setIcon(R.mipmap.icon)//设置标题的图片.setTitle("我是对话框")//设置对话框的标题.setMessage("我是对话框的内容")//设置对话框的内容//设置对话框的按钮.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();dialog.dismiss();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();dialog.dismiss();}}).create();dialog.show();
  • 列表对话框
        final String items[] = {
   "我是Item一", "我是Item二", "我是Item三", "我是Item四"};AlertDialog dialog = new AlertDialog.Builder(this).setIcon(R.mipmap.icon)//设置标题的图片.setTitle("列表对话框")//设置对话框的标题.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).create();dialog.show();
  • 单选列表对话框
final String items[] = {
   "我是Item一", "我是Item二", "我是Item三", "我是Item四"};AlertDialog dialog = new AlertDialog.Builder(this).setIcon(R.mipmap.icon)//设置标题的图片.setTitle("单选列表对话框")//设置对话框的标题.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).create();dialog.show();
  • 多选列表对话框
final String items[] = {
   "我是Item一", "我是Item二", "我是Item三", "我是Item四"};final boolean checkedItems[] = {
   true, false, true, false};AlertDialog dialog = new AlertDialog.Builder(this).setIcon(R.mipmap.icon)//设置标题的图片.setTitle("多选对话框")//设置对话框的标题.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {checkedItems[which] = isChecked;}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {for (int i = 0; i < checkedItems.length; i++) {if (checkedItems[i]) {Toast.makeText(MainActivity.this, "选中了" + i, Toast.LENGTH_SHORT).show();}}dialog.dismiss();}}).create();dialog.show();
  • 完全自定义的对话框

1.首先我们需要自定义Dialog的style,让他自己本有的东西全部透明,然后在设置我们自己的内容就可以达到完全自定义的效果了。

<!--对话框的样式--><style name="NormalDialogStyle"><!--对话框背景 --><item name="android:windowBackground">@android:color/transparent</item><!--边框 --><item name="android:windowFrame">@null</item><!--没有标题 --><item name="android:windowNoTitle">true</item><!-- 是否浮现在Activity之上 --><item name="android:windowIsFloating">true</item><!--背景透明 --><item name="android:windowIsTranslucent">false</item><!-- 是否有覆盖 --><item name="android:windowContentOverlay">@null</item><!--进出的显示动画 --><item name="android:windowAnimationStyle">@style/normalDialogAnim</item><!--背景变暗--><item name="android:backgroundDimEnabled">true</item></style><!--对话框动画--><style name="normalDialogAnim" parent="android:Animation"><item name="@android:windowEnterAnimation">@anim/normal_dialog_enter</item><item name="@android:windowExitAnimation">@anim/normal_dialog_exit</item></style>

2.接下来就可以为对话框设置我们自定义的style了.

 /*** 自定义对话框*/private void customDialog() {final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);View view = View.inflate(this, R.layout.dialog_normal, null);TextView cancel = (TextView) view.findViewById(R.id.cancel);TextView confirm = (TextView) view.findViewById(R.id.confirm);dialog.setContentView(view);//使得点击对话框外部不消失对话框dialog.setCanceledOnTouchOutside(true);//设置对话框的大小view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));Window dialogWindow = dialog.getWindow();WindowManager.LayoutParams lp = dialogWindow.getAttributes();lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.75f);lp.height = WindowManager.LayoutParams.WRAP_CONTENT;lp.gravity = Gravity.CENTER;dialogWindow.setAttributes(lp);cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});dialog.show();}

这里写图片描述

  相关解决方案