Android开发之Dialog\popupindow\windowManager的区别及自定义方法详解

在Android开发中,有三种可以弹出的窗体可以使用,一种是Dialog,一种是popupindow,还有一种是最原始的方法,我估计也是dialog和popupwindow的实现原理,用windowManger添加自定义view窗体。


一、Dialog和popupwindow的区别

这两者都是弹窗,但有本质上的区别。Dialog是非阻塞式弹窗,Dialog弹出来后,后台还在做其他事情。而当Popupwindow弹出的时候,程序阻塞,后台停止运动,只有当我们调用了dismiss方法之后,程序才会继续向下执行。



二、Dialog和popupwindow的自定义方法

1、Dialog的自定义方法:

Dialog的自定义方法有几种,可以继承Dialog类然后setContentView();这里主要讲解运用AlertDialog.Builder来创建自定义对话框的方法。

直接上代码:

// 新建一个buidler
		AlertDialog.Builder builder = new Builder(this);
		// 填充自定义view
		View view = View.inflate(this, R.layout.freedom, null);
		// 创建dialog
		final AlertDialog dialog = builder.create();
		// 设定自定义View,后面跟的值必须为0
		dialog.setView(view, 0, 0, 0, 0);
		// 查找组件
		confirm = (Button) view.findViewById(R.id.confirm);
		cancle = (Button) view.findViewById(R.id.cancle);
		password = (EditText) view.findViewById(R.id.password);
		passwordConfirm = (EditText) view.findViewById(R.id.password_confirm);
		// 组件设定动作监听
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String pwd = password.getText().toString().trim();
				String pwd_confirm = passwordConfirm.getText().toString()
						.trim();
				if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd_confirm)) {
					Toast.makeText(getApplicationContext(), "", 0).show();
					return;
				}
				if (!pwd.equals(pwd_confirm)) {
					Toast.makeText(getApplicationContext(), "", 0).show();
					return;
				}
				Editor editor = sp.edit();
				editor.putString("password", Md5Utils.encode(pwd));
				editor.commit();
				dialog.dismiss();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
		// 可以不设定一下属性,默认是包裹自定义View组件的大小
		// LayoutParams attributes = dialog.getWindow().getAttributes();
		// attributes.gravity = Gravity.CENTER;
		// attributes.width = 100;
		// attributes.height = 100;
		// dialog.getWindow().setAttributes(attributes);

其实在create之后,还可以这样定义:

 final AlertDialog dialog = new AlertDialog.Builder(this).create();
		 dialog.show();
		 Window window = dialog.getWindow();
		        // *** 主要就是在这里实现这种效果的.
		        // 设置窗口的内容页面,<span style="font-family: Arial, Helvetica, sans-serif;">freedom</span><span style="font-family: Arial, Helvetica, sans-serif;">.xml文件中定义view内容</span>
		 window.setContentView(R.layout.freedom);
		        // 为确认按钮添加事件,执行退出应用操作
		 ImageButton confirm = (ImageButton) window.findViewById(R.id.confirm);
		 confirm.setOnClickListener(new View.OnClickListener() {
		  public void onClick(View v) {
		  
		  }
		 });
		 
		        // 关闭alert对话框架
		        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
		        cancel.setOnClickListener(new View.OnClickListener() {
		   public void onClick(View v) {
			   dialog.cancel();


2、PopUpwindow

这个比Dialog的使用要方便得很多。直接上代码:

/**
	 * @Title: showPopWindow
	 * @Description: 弹窗
	 * @param view 自定义View视图
	 * @param x  宽
	 * @param y  高
	 * @param v  挂载窗体
	 * @param gravity 显示方位
	 * @param atX  X方向偏移量
	 * @param atY  Y方向偏移量
	 * @param isClose  是否设定为点击窗体外部,窗体自动关闭
	 * @return
	 * @throws
	 */
	public PopupWindow showPopWindow(View view, int x, int y, View v,
			int gravity, int atX, int atY, boolean isClose) {
		PopupWindow popupWindow = new PopupWindow(view, DptoPxUtil.dip2px(
				context, x), DptoPxUtil.dip2px(context, y), true);
		if (isClose) {
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
		}
		popupWindow.setAnimationStyle(R.style.round_container);
		popupWindow.showAtLocation(v, gravity, atX, atY);

		return popupWindow;

	}

除了showAtlocation外,我们也可以设定它在展示在某一个空间的下方,调用方法popupWindow.showAsDropDown(anchor, xoff, yoff, gravity)即可。


3、WindowManger

调用windowMnager添加窗体,应该是所有弹窗的实现原理,直接上代码:

rrentView = LayoutInflater.from(context).inflate(
				R.layout.round_container, null);
windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		layoutParams = new WindowManager.LayoutParams();
		layoutParams.horizontalMargin = layoutParams.verticalMargin = 0;
		layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
		layoutParams.x = x;
		layoutParams.y = y;
		layoutParams.width = width;
		layoutParams.height = height;
		layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		layoutParams.format = PixelFormat.TRANSLUCENT;
		layoutParams.windowAnimations = 0;
		windowManager.addView(currentView, layoutParams);
			
			
		//windowManager.updateViewLayout(currentView, layoutParams);//更新当前view的位置


关于三种弹窗方式的动画效果已经在昨天讲过,今天就不在描述。

希望能帮助到看到此文章的人。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章