`

AlertDialog和PopupWindow

 
阅读更多
区别:AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。这两种区别的表现是:AlertDialog弹出时,背景是黑色的,但是当我们点击背景,AlertDialog会消失,证明程序不仅响应AlertDialog的操作,还响应其他操作,其他程序没有被阻塞,这说明了AlertDialog是非阻塞式对话框;PopupWindow弹出时,背景没有什么变化,但是当我们点击背景的时候,程序没有响应,只允许我们操作PopupWindow,其他操作被阻塞。

其实PopupWindow在设置了背景图片以后,同样可以点击背景消失.

问题:有个问题一直没有解决,当PopupWindow有背景图片,并且带了动画效果以后,isShowing()会有异常情况出现.

关于这块,专门敲了一些示例代码,之前一阵子很疑惑有些PopupWindow带一个小箭头,有些就没有,研究之后才发现原来是背景图片的原因,瞬间就醉了- - .//

alertdialg

@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.pop_no_img_a://系统自带
//			简单的android    AlertDialog
			new AlertDialog.Builder(this)
			 .setTitle("标题") 
			 .setMessage("简单消息框")
			 	.setPositiveButton("确定", null)
			 	.show();
			break;
		case R.id.pop_no_img_b://自定义
			final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.show();
			Window window = alertDialog.getWindow();
			window.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
			//设置透明度
			WindowManager.LayoutParams lp = window.getAttributes();
			lp.alpha = 0.6f;
			window.setContentView(R.layout.activity_alertdialog);
			break;
		case R.id.pop_no_img_c://自定义附带动画
			final AlertDialog alertDialog1 = new AlertDialog.Builder(this).create();
			alertDialog1.show();
			Window window1 = alertDialog1.getWindow();
			window1.setGravity(Gravity.TOP);
			window1.setWindowAnimations(R.style.AnimationFade);//动画效果
			window1.setContentView(R.layout.activity_alertdialog);
			break;
		default:
			break;
		}
	}


PopupWindow_noimgActivity   没有图片
package com.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.dialogandpopupwindow.R;
import com.example.myview.MyButton;

public class PopupWindow_noimgActivity extends Activity implements OnClickListener{
	
	private View view;
	private PopupWindow popupWindow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pop_no_img);
		initPopUpWindow();
		findViewById(R.id.pop_no_img_a).setOnClickListener(this);
		findViewById(R.id.pop_no_img_b).setOnClickListener(this);
		findViewById(R.id.pop_no_img_c).setOnClickListener(this);
	} 

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.pop_no_img_a:
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAsDropDown(arg0);
			}
			break;
		case R.id.pop_no_img_b:
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAsDropDown(arg0, 50, 100);
			}
			break;
		case R.id.pop_no_img_c:
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAtLocation(findViewById(R.id.pop_no_main) , Gravity.CENTER, 0, 0);
			}
			break;
		case R.id.pop_no_mubut1:
			Toast.makeText(this, "删除", Toast.LENGTH_LONG).show();
			break;
		case R.id.pop_no_mubut2:
			Toast.makeText(this, "网络", Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
	}
	
	private void initPopUpWindow() {
		view = this.getLayoutInflater().inflate(R.layout.pop_no_img, null);
		popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		MyButton myButton = (MyButton) view.findViewById(R.id.pop_no_mubut1);
		myButton.setImageSource(R.drawable.menu_delete);
		myButton.setTextview("删除");
		myButton.setOnClickListener(this);
		MyButton myButton1 = (MyButton) view.findViewById(R.id.pop_no_mubut2);
		myButton1.setImageSource(R.drawable.menu_net);
		myButton1.setTextview("网络");
		myButton1.setOnClickListener(this);
	}
	
}


带图片

package com.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.dialogandpopupwindow.R;
import com.example.myview.MyButton;

public class PopupWindow_withimgActivity extends Activity implements OnClickListener{
	
	private View view;
	private PopupWindow popupWindow;
	
	private Button but1,but2,but3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pop_no_img);
		initPopUpWindow();
		but1 = (Button) findViewById(R.id.pop_no_img_a);
		but2 = (Button) findViewById(R.id.pop_no_img_b);
		but3 = (Button) findViewById(R.id.pop_no_img_c);
		but1.setOnClickListener(this);
		but1.setText("控件下方显示");
		but2.setOnClickListener(this);
		but2.setText("侧滑出现退出");
		but3.setOnClickListener(this);
		but3.setText("中间出现消失");
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.pop_no_img_a:
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAsDropDown(arg0);
			}
			break;
		case R.id.pop_no_img_b:
			popupWindow.setAnimationStyle(R.style.AnimationFade);
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAsDropDown(arg0, 50, 100);
				popupWindow.update();
			}
			break;
		case R.id.pop_no_img_c:
			popupWindow.setAnimationStyle(R.style.AnimationPpo);
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAtLocation(findViewById(R.id.pop_no_main) , Gravity.CENTER, 0, 0);
				popupWindow.update();
			}
			break;
		case R.id.pop_no_mubut1:
			Toast.makeText(this, "删除", Toast.LENGTH_LONG).show();
			break;
		case R.id.pop_no_mubut2:
			Toast.makeText(this, "网络", Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
	}
	
	private void initPopUpWindow() {
		view = this.getLayoutInflater().inflate(R.layout.pop_no_img, null);
		popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		MyButton myButton = (MyButton) view.findViewById(R.id.pop_no_mubut1);
		myButton.setImageSource(R.drawable.menu_delete);
		myButton.setTextview("删除");
		myButton.setOnClickListener(this);
		MyButton myButton1 = (MyButton) view.findViewById(R.id.pop_no_mubut2);
		myButton1.setImageSource(R.drawable.menu_net);
		myButton1.setTextview("网络");
		myButton1.setOnClickListener(this);
		
		// 控制popupwindow点击屏幕其他地方消失
		// 设置背景图片,不能在布局中设置,要通过代码来设置
		popupWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
		// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上
		popupWindow.setOutsideTouchable(true);
	}

}





分享到:
评论

相关推荐

    PopupWindow自定义位置显示的实现代码

    关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindow,当然网上也有使用Activity并配合Dialog主题的方式实现弹窗,有兴趣的朋友也可以去研究一下。对于AlertDialog和PopupWindow两者最主要的区别就是显示的...

    AlertDialog

    AlertDialog 各种各样的对话框和结合popupwindow使用

    DialogFragment

    为什么android系统有AlertDialog,PopupWindow对话框,基本满足客户需求,为啥还要跑出一个DialogFragment对话框呢?这就要从DialogFragment的优点说起了:

    史上最全面的PopupWindow总结

    史上最全面的PopupWindow总结!详细总结了开发PopUpWindow中遇到的各种问题。

    DialogFragment,解决PopupWindow中EditText无法复制粘贴问题

    DialogFragment,解决PopupWindow中EditText无法复制粘贴问题。Blog地址:https://blog.csdn.net/qq_37077360/article/details/83505119

    一个类似UC右上角的弹出菜单(使用PopupWindow)

    一个类似UC右上角的弹出菜单(使用PopupWindow)实例

    Menu和AlertDialog对话框

    NULL 博文链接:https://676744379-qq-com.iteye.com/blog/1122485

    Android之用PopupWindow实现弹出菜单

    在使用UC-WebBrowser时,你会发现它...其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一个是菜单选项。菜单选项视图的切换可以通过适配器的变换,轻松地实现。

    PopupWindow

    PopupWindow使用,简单详细,很多被用在二级菜单等处!能达到AlertDialog的效果!

    Android入门第十篇之PopupWindow.doc

    介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程或者PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该...

    Android入门之PopupWindow用法实例解析

    PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。 先贴出本例中运行的结果图: main.xml的源码如下: <?xml version=1.0 encoding=utf-8?> <LinearLayout ...

    PopUpWindow.zip

    为什么用PopupWindow?PopupWindow相较于AlertDialog,可以很方便的指定要显示的位置,如相对某个控件的位置,相对父容器的位置。

    Android编程实现的自定义弹窗(PopupWindow)功能示例

    在开发过程中,如果要弹出一个对话框,一般是使用AlertDialog,但其使用限制太大,灵活性不够,所以我们常需要用到灵活性更高的PopupWindow, 如图,当点击显示的时候,就会弹出一个对话框,当点击确定或屏幕其它...

    PopUpWindow亲测全网最低积分下载.zip

    PopupWindow相较于AlertDialog,可以很方便的指定要显示的位置,如相对某个控件的位置,相对父容器的位置。

    指尖疯Android4.4视频第10集:对话框系列组件

    对话是上天赋予我们的技能,从哇哇学语开始,我们就通过各种语言和周围环境进行对话交流。对话让人们间更加理解、协作和... AlertDialog 4. PopupWindow。有了本集的知识,APP就能很好的和人类进行交流,是不是很神奇?

    Android入门第九篇之AlertDialog.doc

    AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。先贴出程序运行的截图:main.xml的源码:viewplaincopytoclipboardprint?01....

    Android Studio目前所学习的9个控件代码

    Android Studio目前所学习的9个控件代码 后续随着学习进度还会不断更新 TextView Button EditText ImageView ProgressBar Notification Toolbar AlertDialog PopupWindow

    Android中使用PopupWindow 仿微信点赞和评论弹出

    PopupWindow,弹出框使用PopupWindow实现,这是点赞和评论的载体,具体要涉及 PopupWindow 点击非窗口位置和再次点击消失以及显示位置的问题(根据相应更多按钮的位置确定 PopupWindow 的显示位置 package ...

    手写签名系统的设计与实现之实现手写画板(三、四)

    1、对话款我们用的是popupwindow,不是alertdialog对话框,两者是有区别的:前者是阻塞型,即popupwindow会阻塞主线程,当popupwindow弹出来后,主线程暂停工作,只有popupwindow退出后,主线程才会恢复;...

Global site tag (gtag.js) - Google Analytics