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