Dialog消息弹窗和 PopupWindow弹出试菜单

AI摘要
坤少博客
1,默认的弹窗事件
//创建弹窗对象
AlertDialog.Builder 弹窗 = new AlertDialog.Builder(MainActivity.this);
//设置弹窗标题
弹窗.setTitle("这是标题");
//设置弹窗图标
弹窗.seticon(R.drawable.ic_launcher);
//设置弹窗内容
弹窗.setMessage("这是弹窗内容");
//设置弹窗按钮(右)
弹窗.setPositiveButton("这是弹窗按钮(右)", new DialogInterface.OnClickListener(){                              

							@Override
							public void onClick(DialogInterface p1, int p2)
							{
								//为确定按钮(右)添加点击事件
							}
						});
	//设置弹窗按钮(中)
弹窗.setNegativeButton("这是弹窗按钮(中)", new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface p1, int p2)
							{
									//为确定按钮(中)添加点击事件
							}
						});
	//设置弹窗按钮(左)
弹窗.setNeutralButton("这是弹窗按钮(左)", new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface p1, int p2)
							{
									//为确定按钮(左)添加点击事件
							}
						});
//显示弹窗
弹窗.show();

2,弹窗列表的事件
//多维数组
String[] 按钮 = new String[] {"第一个按钮","第二个按钮","第三个按钮"};
//创建列表弹窗对象
AlertDialog.Builder 列表弹窗 = new AlertDialog.Builder(MainActivity.this);
//创建弹窗列表
列表弹窗.setItems(按钮, new DialogInterface.OnClickListener (){

												@Override
												public void onClick (DialogInterface p1, int p2)
													{
														/*下面列表事件*/
														//下标从0开始
														//用if判断点击的列表是否为当前数
														if(0==p2)
														{
														//列表一的点击事件
														}
														else if(1==p2)
														{
														//列表二的点击事件
														}
														else if(2==p2)
														{
														//列表三的点击事件
														}
													}
										});
										
3,弹窗单选事件
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
					builder.setIcon(R.drawable.ic_launcher);
					builder.setTitle("请选择性别");
					final String[] sex = {"男", "女", "未知性别"};
					//    设置一个单项选择下拉框
					/**
					 * 第一个参数指定我们要显示的一组下拉单选框的数据集合
					 * 第二个参数代表索引,指定默认哪一个单选框被勾选上,1表示默认'女' 会被勾选上
					 * 第三个参数给每一个单选项绑定一个监听器

PopupWindow

1,PopupWindow与AlertDialog的区别
最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

// 构建泡泡窗口:
ViewGroup pop = (ViewGroup)getLayoutInflater().inflate(R.layout.popupwindows_pro,null);

PopupWindow popupWindow = new PopupWindow(pop, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//获取焦点
popupWindow.setFocusable(true);

//设置空白背景,这样点空白部分就会关闭
popupWindow.setBackgroundDrawable(new BitmapDrawable());

//设置进出动画:
popupWindow.setAnimationStyle(R.style.anim);
//在styles.xml中定义动画样式
<style name="anim">
	<item name="android:windowEnterAnimation">@anim/popwindow_in</item>
	<item name="android:windowExitAnimation">@anim/popwindow_out</item>
</style>

//根据父元素位置显示,x,y是偏移距离
View pView = (View) button.getParent();
popupWindow.showAtLocation(pView, Gravity.CENTER, x, y);

//在view组件下方显示,不够会显示在上方
popupWindow.showAsDropDown(view, x, y);

//关闭泡泡窗口:
popupWindow.dismiss();

//设置键盘事件可控制按back键关闭
pop.setOnKeyListener();
文章最后更新时间:2025-06-25 22:43:47,若有错误或已失效,请在下方 留言
© 版权声明
THE END
喜欢就支持一下吧
点赞296 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容