下面列出了android.widget.PopupWindow#setOutsideTouchable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 复制、删除窗体显示
* @author leibing
* @createTime 2017/5/5
* @lastModify 2017/5/5
* @param v 位置参照物
* @param onClick 点击事件
* @return
*/
private void showPopUps(View v, View.OnClickListener onClick){
View popLayout = LayoutInflater.from(this).inflate(R.layout.pop_jk_chat, null);
TextView copyTv = (TextView) popLayout.findViewById(R.id.tv_pop_jk_copy);
TextView delTv = (TextView) popLayout.findViewById(R.id.tv_pop_jk_del);
copyTv.setOnClickListener(onClick);
delTv.setOnClickListener(onClick);
popupWindow = new PopupWindow(popLayout, 240, 100);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + v.getMeasuredWidth() / 3,
location[1]-popupWindow.getHeight());
}
@SuppressWarnings("deprecation")
public SelectRemindCyclePopup(Context context) {
mContext = context;
mPopupWindow = new PopupWindow(context);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
mPopupWindow.setHeight(WindowManager.LayoutParams.FILL_PARENT);
mPopupWindow.setTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setAnimationStyle(R.style.AnimBottom);
mPopupWindow.setContentView(initViews());
mPopupWindow.getContentView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mPopupWindow.setFocusable(false);
// mPopupWindow.dismiss();
return true;
}
});
}
public static void showWxShareDialog(Context context, View parent, Article article) {
PopupWindow popupWindow = new PopupWindow(LayoutInflater.from(context).inflate(R.layout.wechat_share, null), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
View.OnClickListener listener = (View v) -> {
switch (v.getId()) {
case R.id.share_friends:
WxShareUtils.getInstance().shareToWeChat(context, article.getLink(), article.getTitle(), article.getDesc(), 0);
break;
case R.id.share_timeline:
WxShareUtils.getInstance().shareToWeChat(context, article.getLink(), article.getTitle(), article.getDesc(), 1);
break;
default:
Log.e("WxShareDialg", "no this view" + v.getId());
}
popupWindow.dismiss();
};
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(context.getDrawable(R.drawable.wx_share_bg));
popupWindow.getContentView().findViewById(R.id.share_friends).setOnClickListener(listener);
popupWindow.getContentView().findViewById(R.id.share_timeline).setOnClickListener(listener);
popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
}
private void initFloatingWindow() {
mWindow = new PopupWindow(mContext);
mWindow.setFocusable(false);
mWindow.setBackgroundDrawable(null);
mWindow.setOutsideTouchable(true);
mAnimStyle = android.R.style.Animation;
}
private void initmPopupWindowViewright() {
View customView = getActivity().getLayoutInflater().inflate(R.layout.right_top,
null, false);
// 创建PopupWindow实例,200,150分别是宽度和高度
popupWindow = new PopupWindow(customView, 500, 800);
// 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
popupWindow.setAnimationStyle(R.style.ways);
popupWindow.setOutsideTouchable(true);
// 自定义view添加触摸事件
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
/** 在这里可以实现自定义视图的功能 */
Button btn_appsort = (Button) customView.findViewById(R.id.btn_appsort);
// Button btn_netusesort = (Button) customView.findViewById(R.id.btn_netusesort);
Button btn_netlimitsort = (Button) customView.findViewById(R.id.btn_netlimitsort);
// Button btn_wifiusesort = (Button) customView.findViewById(R.id.btn_wifisort);
Button btn_wifilimitsort = (Button) customView.findViewById(R.id.btn_wifilimitsort);
btn_appsort.setOnClickListener(this);
// btn_netusesort.setOnClickListener(this);
btn_netlimitsort.setOnClickListener(this);
// btn_wifiusesort.setOnClickListener(this);
btn_wifilimitsort.setOnClickListener(this);
}
public PopuWindowView(Context mContext, int widthGravity) {
this.mContext = mContext;
LayoutInflater inflater = LayoutInflater.from(mContext);
viewItem = inflater.inflate(R.layout.dialogui_popu_options, null);
pupoListView = (ListView) viewItem.findViewById(R.id.customui_list);
mPopuWindowAdapter = new PopuWindowAdapter(mContext, popuLists);
pupoListView.setAdapter(mPopuWindowAdapter);
pullDownView = new PopupWindow(viewItem, widthGravity,
LayoutParams.WRAP_CONTENT, true);
pullDownView.setOutsideTouchable(true);
pullDownView.setBackgroundDrawable(new BitmapDrawable());
pupoListView.setOnItemClickListener(this);
}
private void initFloatingWindow() {
mWindow = new PopupWindow(mContext);
mWindow.setFocusable(false);
mWindow.setBackgroundDrawable(null);
mWindow.setOutsideTouchable(true);
mAnimStyle = android.R.style.Animation;
}
private void showShareLayout() {
mSharePopWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
View view = LayoutInflater.from(this).inflate(R.layout.sharelayout, null);
view.findViewById(R.id.btn_share_to_circle).setOnClickListener(this);
view.findViewById(R.id.btn_share_to_friend).setOnClickListener(this);
mSharePopWindow.setContentView(view);
mSharePopWindow.setFocusable(false);
mSharePopWindow.setBackgroundDrawable(new BitmapDrawable());
mSharePopWindow.setOutsideTouchable(true);
mSharePopWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM,0,0);
}
/**
* 添加修改重复
*/
private void showRepeat() {
ListView v = new ListView(context);
v.setFooterDividersEnabled(false);
final PopupWindow popupWindow = new PopupWindow(v, PixelUtil.dp2px(100), PixelUtil.dp2px(180));
popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.card_bg));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true); // 点击popWin
popupWindow.setOutsideTouchable(true); // 以处的区域,自动关闭
int[] location = new int[2];
tv_repeat.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] + tv_repeat.getHeight());
final List<String> list = new ArrayList<>();
list.add("单次");
list.add("每天");
list.add("每周");
list.add("每月");
v.setAdapter(new ArrayAdapter<String>(context, R.layout.item_list_simple, list));
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
task.setRepeat(position);
tv_repeat.setText(list.get(position));
popupWindow.dismiss();
}
});
}
@SuppressWarnings("deprecation")
private MessageOperatePopup(Context ctx, View parent) {
View view = LayoutInflater.from(ctx).inflate(R.layout.tt_popup_list,
null);
this.context = ctx;
// popView = (LinearLayout) view.findViewById(R.id.popup_list);
copyBtn = (TextView) view.findViewById(R.id.copy_btn);
copyBtn.setOnClickListener(this);
copyBtn.setOnTouchListener(this);
copyBtn.setPadding(0, 13, 0, 8);
resendBtn = (TextView) view.findViewById(R.id.resend_btn);
resendBtn.setOnClickListener(this);
resendBtn.setOnTouchListener(this);
resendBtn.setPadding(0, 13, 0, 8);
speakerBtn = (TextView) view.findViewById(R.id.speaker_btn);
speakerBtn.setOnClickListener(this);
speakerBtn.setOnTouchListener(this);
speakerBtn.setPadding(0, 13, 0, 8);
mWidth = (int) context.getResources().getDimension(
R.dimen.message_item_popup_width_single_short);
mHeight = (int) context.getResources().getDimension(
R.dimen.message_item_popup_height);
int[] location = new int[2];
parent.getLocationOnScreen(location);
mParentTop = location[1];
mPopup = new PopupWindow(view, mWidth, mHeight);
// mPopup.setFocusable(true);
// 设置允许在外点击消失
mPopup.setOutsideTouchable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
mPopup.setBackgroundDrawable(new BitmapDrawable());
}
private void initFloatingWindow() {
mWindow = new PopupWindow(mContext);
mWindow.setFocusable(false);
mWindow.setBackgroundDrawable(null);
mWindow.setOutsideTouchable(true);
mAnimStyle = android.R.style.Animation;
}
private PopupWindow getPopupWindow(View popupView) {
PopupWindow mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
mPopupWindow.setAnimationStyle(R.style.jc_popup_toast_anim);
return mPopupWindow;
}
/**
* 初始化popupwindow
*/
private void initialPopups() {
LayoutInflater inflater = LayoutInflater.from(this);
// 引入窗口配置文件
View view = inflater.inflate(R.layout.third_image_popupwindows, null);
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// 创建PopupWindow对象
pops = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, false);
pops.setOnDismissListener(this);
rl_pop_nulls = (RelativeLayout) view
.findViewById(R.id.third_popupwindow_layout_nulls);
tv_pop_quxiaos = (TextView) view
.findViewById(R.id.third_popupwindow_textView_quxiaoo);
tv_pop_photo = (TextView) view
.findViewById(R.id.third_popupwindow_textView_photo);
tv_pop_camera = (TextView) view
.findViewById(R.id.third_popupwindow_textView_camera);
rl_pop_nulls.setOnClickListener(this);
tv_pop_quxiaos.setOnClickListener(this);
tv_pop_photo.setOnClickListener(this);
tv_pop_camera.setOnClickListener(this);
// 需要设置一下此参数,点击外边可消失
pops.setBackgroundDrawable(new BitmapDrawable());
// 设置点击窗口外边窗口消失
pops.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
pops.setFocusable(true);
initialPopupss();
}
private void initmPopupWindowViewleft() {
View customView = getLayoutInflater().inflate(R.layout.netleft_top,
null, false);
popupWindow = new PopupWindow(customView, 500, 600);
// 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
popupWindow.setAnimationStyle(R.style.ways);
popupWindow.setOutsideTouchable(true);
// 自定义view添加触摸事件
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
/** 在这里可以实现自定义视图的功能 */
Button btn_wall = (Button) customView.findViewById(R.id.netbtn_wall);
Button btn_bag = (Button) customView.findViewById(R.id.netbtn_bag);
btn_wall.setOnClickListener(this);
btn_bag.setOnClickListener(this);
}
private void initFloatingWindow() {
mWindow = new PopupWindow(mContext);
mWindow.setFocusable(false);
mWindow.setBackgroundDrawable(null);
mWindow.setOutsideTouchable(true);
mAnimStyle = android.R.style.Animation;
}
/**
* 从Activity底部弹出来
*/
public void showAtBottom(Activity activity, View parent, ImageFolderBean curFloder, onFloderItemClickListener listener)
{
this.mActReference = new WeakReference<>(activity);
this.mListener = listener;
View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_image_floder_pop, null);
int height = activity.getResources().getDimensionPixelSize(R.dimen.imagepicker_floder_pop_height);
mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, height, true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setFocusable(true);//设置pop可获取焦点
mPopupWindow.setAnimationStyle(R.style.FloderPopAnimStyle);//设置显示、消失动画
mPopupWindow.setOutsideTouchable(true);//设置点击外部可关闭pop
mPopupWindow.setOnDismissListener(this);
mListView = (ListView) contentView.findViewById(R.id.lv_image_floder_pop);
final int position = ImageDataModel.getInstance().getAllFolderList().indexOf(curFloder);
mAdapter = new ImageFloderAdapter(activity, position);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
toggleWindowAlpha();
// 增加绘制监听
ViewTreeObserver vto = mListView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// 移除监听
mListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mListView.setSelection(position);
}
});
}
private void showPasswordReminder() {
View popupLayout = getLayoutInflater().inflate(R.layout.popup_password, null);
PopupWindow popup = new PopupWindow(popupLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setFocusable(false);
popup.setOutsideTouchable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
popup.setElevation(5.0f);
}
_textPassword.post(() -> popup.showAsDropDown(_textPassword));
_textPassword.postDelayed(popup::dismiss, 5000);
}
private void showPopupWindow() {
//创建对象
PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.view_pop_custom, null);
//设置view布局
popupWindow.setContentView(inflate);
popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
//设置动画的方法
popupWindow.setAnimationStyle(R.style.BottomDialog);
//设置PopUpWindow的焦点,设置为true之后,PopupWindow内容区域,才可以响应点击事件
popupWindow.setTouchable(true);
//设置背景透明
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//点击空白处的时候让PopupWindow消失
popupWindow.setOutsideTouchable(true);
// true时,点击返回键先消失 PopupWindow
// 但是设置为true时setOutsideTouchable,setTouchable方法就失效了(点击外部不消失,内容区域也不响应事件)
// false时PopupWindow不处理返回键,默认是false
popupWindow.setFocusable(false);
//设置dismiss事件
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
boolean showing = popupWindow.isShowing();
if (!showing){
//show,并且可以设置位置
popupWindow.showAsDropDown(mTv1);
}
//popupWindow.dismiss();
}
public void show() {
if (mAnchorView == null) {
throw new IllegalArgumentException("PopupWindow show location view can not be null");
}
if (mItemData == null) {
throw new IllegalArgumentException("please fill ListView Data");
}
mPopView = new ListView(mContext);
mPopView.setBackgroundColor(ContextCompat.getColor(mContext, android.R.color.white));
mPopView.setVerticalScrollBarEnabled(false);
mPopView.setDivider(null);
mPopView.setAdapter(new ArrayAdapter<>(mContext,
android.R.layout.simple_list_item_1, mItemData));
if (mItemClickListener != null) {
mPopView.setOnItemClickListener(mItemClickListener);
}
mPopView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
if (mPopupWindowWidth == 0) {
mPopupWindowWidth = mDeviceWidth / 3;
}
if (mPopupWindowHeight == 0) {
mPopupWindowHeight = mItemData.size() * mPopView.getMeasuredHeight();
if (mPopupWindowHeight > mDeviceHeight / 2) {
mPopupWindowHeight = mDeviceHeight / 2;
}
}
mPopupWindow = new PopupWindow(mPopView, mPopupWindowWidth, mPopupWindowHeight);
if (mPopAnimStyle != 0) {
mPopupWindow.setAnimationStyle(mPopAnimStyle);
}
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(mModal);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), (Bitmap) null));
Rect location = locateView(mAnchorView);
if (location != null) {
int x;
//view中心点X坐标
int xMiddle = location.left + mAnchorView.getWidth() / 2;
if (xMiddle > mDeviceWidth / 2) {
//在右边
x = xMiddle - mPopupWindowWidth;
} else {
x = xMiddle;
}
int y;
//view中心点Y坐标
int yMiddle = location.top + mAnchorView.getHeight() / 2;
if (yMiddle > mDeviceHeight / 2) {
//在下方
y = yMiddle - mPopupWindowHeight;
} else {
//在上方
y = yMiddle;
}
mPopupWindow.showAtLocation(mAnchorView, Gravity.NO_GRAVITY, x, y);
}
}
/**
* 最核心的pupupwindow创建方法
*
* @param contentView popupwindow要显示的视图
* @param width popupwindow的宽度
* @param height popupwindow的高度
* @return
*/
@NonNull
private static PopupWindow getPopupWindow(View contentView, int width, int height) {
PopupWindow popupWindow = new PopupWindow(contentView, width, height, true);
popupWindow.setOutsideTouchable(false);
openOutsideTouchable(popupWindow);
return popupWindow;
}