AndroidStudio中AlertDialog的四种用法

article/2025/10/31 18:40:28

目录

1.默认样式

2.单选弹出框

3.多选弹出框

4.自定义弹出框

补充!!

 


1.默认样式

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder.setTitle("请回答");
builder.setMessage("你觉得我好看吗??");
builder.setPositiveButton("当然是好看了!!", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "嘻嘻嘻",Toast.LENGTH_SHORT).show();}
});
builder.setNeutralButton("我觉得一般", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this,"那你再瞅瞅~",Toast.LENGTH_SHORT).show();}
});
builder.setNegativeButton("我觉得不好看", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this,"嘤嘤嘤",Toast.LENGTH_SHORT).show();}
});
builder.show();

 


2.单选弹出框

final String[] gender = new String[]{"帅哥","美女"};
android.support.v7.app.AlertDialog.Builder builder1=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder1.setTitle("请选择你的性别");
builder1.setItems(gender, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ToastUtil.showMsg(AlertDialogActivity.this,gender[which]);}
});
builder1.show();

 

android.support.v7.app.AlertDialog.Builder builder2=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder2.setTitle("你最喜欢吃一下那种菜肴:");
final String[] dish =new String[]{"红烧牛肉","粉蒸排骨","油焖大虾","蒜蓉扇贝"};
builder2.setSingleChoiceItems(dish, 1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ToastUtil.showMsg(AlertDialogActivity.this,dish[which]);dialog.dismiss();}
});
builder2.setCancelable(false);
builder2.show();
break;

 


3.多选弹出框

String[] dessert = new String[]{"抹茶千层","芒果拿破仑","草莓雪媚娘","蓝莓慕斯"};
boolean[] begin = new boolean[]{false,false,false,false};
android.support.v7.app.AlertDialog.Builder builder3=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this);
builder3.setTitle("你吃过以下哪些甜点");
builder3.setMultiChoiceItems(dessert, begin, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {}
});
builder3.setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ToastUtil.showMsg(AlertDialogActivity.this,"好的,我知道啦!");}
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ToastUtil.showMsg(AlertDialogActivity.this,"难道你都没有吃过??");}
});
builder3.show();
break;

 


4.自定义弹出框

自定义Dialog的xml样式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fff"android:orientation="vertical"><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="提示"android:textSize="20sp"android:textColor="#000"android:textStyle="bold"android:layout_marginTop="20dp"/><TextViewandroid:id="@+id/tv_message"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="确定要删除订单吗?"android:textSize="20sp"android:textColor="#000"android:layout_marginTop="20dp"/><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginTop="20dp"android:background="#414E5E"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/bt_cancel"android:layout_width="0dp"android:layout_weight="1"android:text="取消"android:background="@drawable/bt_dialog"android:textColor="#14BFB1"android:textSize="20sp"android:layout_height="wrap_content" /><Viewandroid:layout_width="0.5dp"android:background="#414E5E"android:layout_height="match_parent" /><Buttonandroid:id="@+id/bt_confirm"android:layout_width="0dp"android:layout_weight="1"android:text="确认"android:background="@drawable/bt_dialog"android:textColor="#14BFB1"android:textSize="20sp"android:layout_height="wrap_content" /></LinearLayout>
</LinearLayout>

自定义Dialog组件,CustomDialog.java

public class CustomDialog extends Dialog implements View.OnClickListener {//声明xml文件里的组件private TextView tv_title,tv_message;private Button bt_cancel,bt_confirm;//声明xml文件中组件中的text变量,为string类,方便之后改private String title,message;private String cancel,confirm;//声明两个点击事件,等会一定要为取消和确定这两个按钮也点击事件private IOnCancelListener cancelListener;private IOnConfirmListener confirmListener;//设置四个组件的内容public void setTitle(String title) {this.title = title;}public void setMessage(String message) {this.message = message;}public void setCancel(String cancel,IOnCancelListener cancelListener) {this.cancel = cancel;this.cancelListener=cancelListener;}public void setConfirm(String confirm,IOnConfirmListener confirmListener){this.confirm=confirm;this.confirmListener=confirmListener;}//CustomDialog类的构造方法public CustomDialog(@NonNull Context context) {super(context);}public CustomDialog(@NonNull Context context, int themeResId) {super(context, themeResId);}//在app上以对象的形式把xml里面的东西呈现出来的方法!@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//为了锁定app界面的东西是来自哪个xml文件setContentView(R.layout.custom_dialog);//设置弹窗的宽度WindowManager m = getWindow().getWindowManager();Display d = m.getDefaultDisplay();WindowManager.LayoutParams p =getWindow().getAttributes();Point size = new Point();d.getSize(size);p.width = (int)(size.x * 0.8);//是dialog的宽度为app界面的80%getWindow().setAttributes(p);//找到组件tv_title=findViewById(R.id.tv_title);tv_message=findViewById(R.id.tv_message);bt_cancel=findViewById(R.id.bt_cancel);bt_confirm=findViewById(R.id.bt_confirm);//设置组件对象的text参数if (!TextUtils.isEmpty(title)){tv_title.setText(title);}if (!TextUtils.isEmpty(message)){tv_message.setText(message);}if (!TextUtils.isEmpty(cancel)){bt_cancel.setText(cancel);}//为两个按钮添加点击事件bt_confirm.setOnClickListener(this);bt_cancel.setOnClickListener(this);}//重写onClick方法public void onClick(View view) {switch (view.getId()){case R.id.bt_cancel:if(cancelListener!=null){cancelListener.onCancel(this);}dismiss();break;case R.id.bt_confirm:if(confirmListener!=null){confirmListener.onConfirm(this);}dismiss();//按钮按之后会消失break;}}//写两个接口,当要创建一个CustomDialog对象的时候,必须要实现这两个接口//也就是说,当要弹出一个自定义dialog的时候,取消和确定这两个按钮的点击事件,一定要重写!public interface IOnCancelListener{void onCancel(CustomDialog dialog);}public interface IOnConfirmListener{void onConfirm(CustomDialog dialog);}
}

使用自定义的弹出框组件

CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this);
customDialog.setTitle("提醒");
customDialog.setMessage("你确定要删除吗?");
customDialog.setCancel("cancel", new CustomDialog.IOnCancelListener() {@Overridepublic void onCancel(CustomDialog dialog) {Toast.makeText(CustomDialogActivity.this, "取消成功!",Toast.LENGTH_SHORT).show();}
});
customDialog.setConfirm("confirm", new CustomDialog.IOnConfirmListener(){@Overridepublic void onConfirm(CustomDialog dialog) {Toast.makeText(CustomDialogActivity.this, "确认成功!",Toast.LENGTH_SHORT).show();}
});
customDialog.show();


以上代码中用到的ToastUtil类,是我自定义的一个类,目的是把Toast的使用稍微封装一下,偷一下懒。代码如下:

public class ToastUtil {public static Toast toast;public static void showMsg(Context context,String msg){if(toast==null){toast=Toast.makeText(context,msg,Toast.LENGTH_LONG);}else{toast.setText(msg);}toast.show();}
}

如果还有问题,欢迎告诉我哦~


http://chatgpt.dhexx.cn/article/S1dJjMhn.shtml

相关文章

Android之AlertDialog的基础使用

坦白说&#xff0c;AlertDialog我在工作中用得并不多&#xff0c;因为AlertDialog的样式比较固定和呆板&#xff0c;为了和App的整体设计匹配&#xff0c;一般都是使用自定义的Dialog&#xff0c;只有在要求不高时用一下。但是作为Android的基础控件之一&#xff0c;掌握它是十…

安卓AlertDialog弹窗

Android在开发中经常会遇到有弹框的需求。 经常使用的有Dialog 弹框&#xff0c;Window弹框&#xff08;任意位置弹出除了外观样式和显示的位置的区别之外&#xff0c;他们之间最本质的区别是&#xff1a; dialog是非阻塞式对话框&#xff0c;popupwindow是阻塞式对话框。也就…

AlertDialog对话框的简单使用

目录 一、对话框的创建 二、单选的对话框 三、多选的对话框 一、对话框的创建 一般的对话框分为标题、内容、按钮三大部分。 常见的方法&#xff1a; 方法功能setTitle()设置对话框的标题setIcon()设置对话框的图标setMessage()设置对话框的提示信息setPositiveButton()设…

alertDialog使用详解

1、设置标题、内容、图标 2、设置按钮 3、使用列表、单选和多选。适配 4、设定弹窗大小 5、自定义view 6、设置点击周边灰色区域弹窗不消失 7、自定义view圆角消除周边白块 1、设置标题、内容、图标 AlertDialog alertDialog new AlertDialog.Builder(this) .setTit…

AlertDialog6种使用方法

AlertDialog 1.AlertDialog的6种创建模式 1.1setMessage 1&#xff09;Java代码 //1.创建构造器AlertDialog.Builder buildernew AlertDialog.Builder(this);//2.设置参数builder.setTitle("弹窗提示").setIcon(R.mipmap.boy).setMessage("选择你的性别&#xf…

Android的AlertDialog详解(7种方式)

需要注意的两点&#xff1a; 1. 在setIcon时&#xff0c;需要使用setTitle方法&#xff0c;否则icon不会显示 2.如果同时调用setMessage 和 setItems(或者setSingleChoiceItems setMultiChoiceItems)函数会导致dialog没有显示内容 AlertDialog的构造方法全部是Protected的&am…

AlertDialog(对话框)详解

AlertDialog可以在当前的界面上显示一个对话框&#xff0c;这个对话框是置顶于所有界面元素之上的&#xff0c;能够屏蔽掉其他控件的交互能力&#xff0c;因此AlertDialog一般是用于提示一些非常重要的内容或者警告信息。 1.创建AlertDialog 首先&#xff0c;我们来了解一下Al…

andoid小游戏开发

apk下载&#xff1a;apk下载&#xff1a;http://download.csdn.net/detail/xiangqiao123/3805861 这是去年用android写的一个小游戏&#xff0c; 我感觉不错&#xff0c;是从事android小游戏开发入门不错的案例&#xff0c; 今天就把它拿出来和大家共享一下。 程序截图&a…

android 2D 游戏的开发的方法

最近学习了android 2D 应用的开发&#xff0c;拿来和大家分享一下&#xff0c;学习2D 开发前我们先了解一下SurfaceView的使用以及贴图技术的使用&#xff0c;最后呢&#xff0c;是一个简单的2的游戏的实现。 1.SurfaceView的一些用法 提供了一个专门的绘图渲染的图形嵌入在一个…

Android手机游戏开发入门教程

Android手机游戏开发入门教程 视频欣赏地址 来自 “ ITPUB博客 ” &#xff0c;链接&#xff1a;http://blog.itpub.net/29597077/viewspace-1139520/&#xff0c;如需转载&#xff0c;请注明出处&#xff0c;否则将追究法律责任。 转载于:http://blog.itpub.net/29597077/vie…

Android 游戏开发速递

作者 / Greg Hartrell, Head of Product Management, Games on Android & Google Play 在今年 3 月举行的 Google 游戏开发者峰会上&#xff0c;我们分享了 Google 为帮助游戏开发者而持续投入研发的数种新工具和服务。这些新工具和服务能够帮助游戏开发者更轻松地查看其 A…

游戏开发相关

游戏开发—图形图像篇 游戏开发--开篇  记得我第一次玩的PC game 是KKND(绝地风暴)&#xff0c;当时的游戏平台是DOS&#xff0c;我只是觉得很好玩&#xff0c;经常和几个小学同学一起厮杀到12点。可是现在回忆起来&#xff0c;KKND无论是从智能设计还是在游戏画面与操作上都…

android小游戏制作基础,View实现游戏布局和方法

在使用android的朋友们&#xff0c;相信大家对android的游戏不陌生吧&#xff0c;像愤怒的小鸟&#xff0c;植物大战僵尸等等优秀的游戏&#xff0c;给我们带来了很好的用户体验 下面我来教大家一点android游戏开发的一点基础&#xff0c;大家可以参照这个方法框架来设计一些像…

android游戏开发的架构

&#xfeff;&#xfeff; 在编写游戏代码之前&#xff0c;必须要仔细地理顺思路&#xff0c;清晰地构建出整个游戏的框架。有的开发者经常抱怨说&#xff0c;游戏开发到最后总是千头万绪&#xff0c;一旦出现bug就不知道该如何修改&#xff0c;身心疲惫甚至是痛不欲生。其实不…

用Unity3d开发Android游戏

Unity3d是个强大的游戏引擎&#xff0c;可以很轻松的将游戏发布到Android平台上&#xff0c;今天我就来讲讲如何用Android来开发Android游戏。 首先我们要下载Android SDK&#xff0c;可以在http://developer.android.com/sdk/index.html这里下载到&#xff0c;运行installer安…

Android游戏开发的入门实例

在Android系统上开发游戏是Android开发学习者所向往的&#xff0c;有成就感也有乐趣&#xff0c;还能取得经济上的报酬。那怎样开发Android游戏呢&#xff1f;下面介绍一个简单的入门实例。 一、创建新工程   首先&#xff0c;我们在Eclipse中新建一个名为Movement的工程&…

如何开发手机游戏?

当今社会&#xff0c;手机游戏无非是当下在旅途中打发时间的最便捷的方式。有关数据显示&#xff0c;62% 的智能手机用户在购买智能手机后的一周内安装了游戏。无论你是参加聚会还是度假&#xff0c;或者周末宅在家里&#xff0c;手机都是玩游戏最方便的设备。这也正是手机游戏…

【Android游戏开发详细过程1】Android平台飞机大战游戏APP设计与实现

【Android游戏】Android平台飞机大战游戏APP设计与实现 前言一、界面设计与功能实现1.1 主界面1.2 登录界面1.2 注册界面1.4 菜单界面1.5 设置界面1.6 商店界面1.7 换机界面1.8 游戏界面1.9 欢迎界面1.10 游戏图标 二、数据库设计与实现三、服务器设计与实现四、其他功能实现 前…

datagridview中使用DataGridViewComboBoxColumn

在datagridview中使用自带的DataGridViewComboBoxColumn&#xff0c;加载数据库中的数据&#xff0c;选中和保存所需要的数据 实现效果如图 加载数据库已保存的数 选择自己所需要的数据 具体代码如下 datagridview命名为&#xff1a;dgvDrugList 添加基本列及对应的设…

DataGridView怎样实现添加、删除、上移、下移一行

场景 在Winform中使用DataGridView实现添加一行、删除一行、上移一行、下移一行。 注&#xff1a; 博客主页&#xff1a;https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 添加一行 private void Task…