自定义PobUpwindow弹出框

在做分享这一块的时候,用到弹出框的功能,还必须得有点击返回键,同样可以撤销显示的弹出框,于是自己就动手写了一个,一切看代码:

public class ShareBoardPicker implements OnClickListener {

	private Context mContext;
	private SharePobupWindow mPopupWindow;//分享框
	private LinearLayout layout;
	private RelativeLayout mWeixinLayout;//微信分享
	private RelativeLayout mWeixinCircleLayout;//微信朋友圈分享
	private RelativeLayout mSinaLayout;//新浪微博分享
	private RelativeLayout mQQZoneLayout;//QQ空间分享
	private UMengShare share;

	public ShareBoardPicker(Context context, String infoStr) {
		mContext = context;
		share = new UMengShare(context,infoStr);
		init();
	}
	public ShareBoardPicker(Context context, String title, String url, String imgUrl) {
		mContext = context;
		share = new UMengShare(context,title,url,imgUrl);
		init();
	}

	public void init(){
		LayoutInflater inflater = LayoutInflater.from(mContext);
		layout = (LinearLayout) inflater.inflate(R.layout.shareboard_picker,
				null);
		mWeixinLayout = (RelativeLayout) layout.findViewById(R.id.weixin);
		mWeixinCircleLayout = (RelativeLayout) layout
				.findViewById(R.id.weixin_circle);
		mSinaLayout = (RelativeLayout) layout.findViewById(R.id.sina);
		mQQZoneLayout = (RelativeLayout) layout.findViewById(R.id.email_share);

		mWeixinLayout.setOnClickListener(this);
		mWeixinCircleLayout.setOnClickListener(this);
		mSinaLayout.setOnClickListener(this);
		mQQZoneLayout.setOnClickListener(this);
		mPopupWindow = new SharePobupWindow(mContext,layout, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT, true);
		// 设置点击窗口外边窗口消失
		mPopupWindow.setOutsideTouchable(true);
		// 设置此参数获得焦点,否则无法点击
		mPopupWindow.setFocusable(true);
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mPopupWindow.setAnimationStyle(R.style.picker_anim_style);
	}
	public void show() {
		WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
				.getAttributes();
		lp.alpha = 0.5f; // 0.0-1.0
		((Activity) mContext).getWindow().setAttributes(lp);
		layout.setFocusable(true);// 设置该view能监听事件
		layout.setFocusableInTouchMode(true);// 设置该view能监听事件
		layout.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && mPopupWindow != null) {
					dismiss();
				}
				return true;
			}
		});
		mPopupWindow.update();
		mPopupWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
	}

	public void dismiss() {
		WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
				.getAttributes();
		lp.alpha = 1.0f; // 0.0-1.0
		((Activity) mContext).getWindow().setAttributes(lp);
		mPopupWindow.dismiss();
		mPopupWindow = null;
		((Activity) mContext).getWindow().setAttributes(lp);
	}
	/**
	 * 用来在Activity中使用
	 *
	 * @return
	 */
	public void activityResult(int requestCode, int resultCode, Intent data) {
		share.activityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.weixin:
			share.startWeixinShare();
			dismiss();
			break;
		case R.id.weixin_circle:
			share.startWeixinCircleShare();
			dismiss();
			break;

		case R.id.sina:
			share.startSinaShare();
			dismiss();
			break;

		case R.id.email_share:
			share.startEmailShare();
			dismiss();
			break;
		default:
			dismiss();
			break;
		}
	}

	class SharePobupWindow extends PopupWindow {

		private Context myContext;
		@Override
		public void dismiss() {
			// TODO Auto-generated method stub
			WindowManager.LayoutParams lp = ((Activity) myContext).getWindow()
					.getAttributes();
			lp.alpha = 1.0f; // 0.0-1.0
			((Activity) myContext).getWindow().setAttributes(lp);
			super.dismiss();
		}

		public SharePobupWindow(){}
		public SharePobupWindow(Context context,View contentView, int width, int height, boolean focusable){
			super(contentView, width, height, focusable);
			myContext = context;
		}
	}
}
时间: 2024-10-20 07:25:39

自定义PobUpwindow弹出框的相关文章

自定义的弹出框列表适配器,类似于大众点评或美团

无意下载一个代码, 还不错,记录一下,说不定以后会用到.效果图如下 整体工程是引入了一个library,当然完全可以写到一个工程里面,如下截图 代码还是很好理解的, 下面贴出来,顺便又加了一些注释 1.MainActivity.java public class MainActivity extends ActionBarActivity { private PopupButton btn; private PopupButton btn2; private LayoutInflater inf

自定义PopupWindow弹出框(带有动画)

使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow 1 public class LostPopupWindow extends PopupWindow { 2 public Lost lost; 3 public void onLost(Lost lost){ 4 this.lost = lost; 5 } 6 private View conentView; 7 8 public View getConentView() { 9 return cone

自定义popup弹出框

ys_popup.css .ys-popup{     position:fixed;     top:0;     bottom:0;     left:0;     right:0;     display:none;     z-index: 99999;     background-color: rgba(0,0,0,0.4); } .ys-popup .ys-popup-content{     position:absolute;     left:30px;     right:

iOS自定义提示弹出框(类似UIAlertView)

菜鸟一枚,大神勿喷.自己在牛刀小试的时候,发现系统的UIAlertView有点不喜欢,然后就自己自定义了一个UIAlertView,基本上实现了系统的UIAlertView,可以根据项目的需求修改UIAlertView的颜色.欢迎大神多多指导.不胜感激! 效果图: Paste_Image.png --------------直接贴代码---------不喜勿喷----------大神多多指导.不胜感激!------- #import <UIKit/UIKit.h> typedef void(^

swing自定义JDialog弹出框

第一次搞swing,自定义JDialog的例子较少,写下来备忘 ,对JDialog中的文本框进行了验证 package com.chauvet; import java.awt.Component;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent

ionic中自定义底部弹出框

通用部分:<script>     MY_FUNCTION = (function() {        var upSheet = document.createElement('div');        upSheet.setAttribute('style', 'display:none;position:absolute;left:0px;top:0px;z-index: 9999;');        upSheet.style.width = document.documentE

自定义弹出框效果

对网站而言,弹出框是比较常见的.或是给出用户操作提示,或是通过弹出框打开一个小窗口以提示信息,或是给出错误警示等等. 但是由于浏览器自带的弹出窗口alert , confirm , prompt样式比较单调,且不同浏览器有不同的默认样式设置. 所以在日常工作中,给网站做一个自定义的弹出框十分必要.特别是富交互的网站 一.提示框 html部分: 1 <!--修改弹窗--> 2 <div class="pop-alert" id="pop" style

AlertDialog自定义View的用法+如何改变弹出框的大小

android系统定义了弹出框,支持我们自定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.custom_message_rename, null); AlertDialog.Builder builder = new AlertDialog.Builder(AnimationTe

鼠标拖动 自定义弹出框

/*设置自定义弹出框可移动*/jQuery(document).ready( function () { $('.popmodal .popheader').mousedown( function (event) { var isMove = true; var abs_x = event.pageX - $(this).offset().left; var abs_y = event.pageY - $(this).offset().top; $(document).mousemove(fun