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);
	}

}





猜你喜欢

转载自18767136122.iteye.com/blog/2117819
今日推荐