关于点击显示PopupWindow再次点击消失的问题

点击一个按钮,弹出一个PopupWindow,想在触摸PopupWindow外区域或是再次点击按钮的时候dismiss此PopupWindow。直接上代码:

//点击事件里的代码

if (mWin == null) {
	initPopupWindow();
}
if (mWin.isShowing()) {//这一句真心没用,isShowing()总是返回false
	Util.write("win is showing");
	mWin.dismiss();
} else {
	Util.write("win is not showing");
	mWin.showAtLocation(resultIv, Gravity.BOTTOM, 0, 0);
	mWin.update();
}

//点击事件代码结束

private void initPopupWindow() {
	View v = View.inflate(this, R.layout.menu_layout, null);
	mWin = new PopupWindow(v, LayoutParams.MATCH_PARENT,
			LayoutParams.WRAP_CONTENT);
	// 设置显示关闭动画
	mWin.setAnimationStyle(R.style.bottom2up);
	// 设置背景,触摸框外区域也可以关闭弹出框
	mWin.setBackgroundDrawable(new ColorDrawable());
	mWin.setOutsideTouchable(true);
	mWin.setFocusable(true);
	mWin.setTouchInterceptor(new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
				mWin.dismiss();
				return true;
			}
			return false;
		}
	});
}

以上代码亲测,在魅族4、android 4.4.2、minSdkVersion 17、targetSdkVersion 22,有效。

时间: 2024-08-04 13:32:31

关于点击显示PopupWindow再次点击消失的问题的相关文章

vue2.0实现点击后显示,再次点击隐藏

描述.点击系统切换,弹出系统切换框.再次点击系统切换,隐藏.点击其他地方.也会隐藏 在layout.vue中写的html代码 1.在main.js中写入全局函数 // 定义全局点击函数,右侧系统切换点击其他地方隐藏系统切换菜单,在layout.vue中使用 Vue.prototype.globalClick = function (callback) { document.onclick = function () { callback(); }; }; 2.在layout.vue中的js部分代

点击按钮内容显示,再次点击隐藏

html代码: <div class="box">领取红包</div> <p class="main on">恭喜您获得300元红包抵用券</p> css: .box{width: 100px;height: 30px;background: #FA6A2F;text-align: center;line-height: 30px;border-radius: 10px;cursor: pointer;color: #

jq点击显示,再点击隐藏

每次都会遇到的问题: <script> $("button").click(function(){ if($(".div").css("display")=="none"){ $(".div").show(); }else{ $(".div").hide(); } }); </script>

jquery点击选中,再次点击取消选中

if(!$(this).hasClass("current")){ $(this).addClass("current"); }else{ $(this).removeClass("current"); }

列表点击当前伸开再次点击收起,点击当前伸开后点击别的列表,别的列表伸开,其余的收起

$(function() { $('section').click(function(){ // 点击当前展开再次点击收起 if($(this).attr('isclick') == 'yes'){ $(this).find('div').css('display','none'); $('section').removeAttr('isclick'); return; } // 点击当前展开别的收起 $('section').find('div').css('display','none');

按钮显示PopupWindow,setOutsideTouchable(true)时,点击按钮再次打开的问题

先给大家看看这个:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E7%9F%A5%E8%AF%86%E5%BA%93/21462.shtml 可以说我也是受到了一点这个的启发,虽然我的思路和这很不一样,记录下. 我没有去试上面的方法,但看着应该也可以,但是我觉得不太妥,所以没有用,大家想法不一样,所以也贴上给大家看看,同时也感谢大神的提醒. 同求有更多,更好想法的人相互分享!! ok,废话不多说,进入正题: 问题是这样,我在界面的个bu

jQuery 点击显示再次点击隐藏

<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> </head> <body> <div> <span class="color">深咖色</span> <div class="cc"></div

android利用PopupWindow实现点击工具栏弹出下拉菜单

1.概述 本文将介绍如何利用PopupWindow实现点击屏幕顶部工具栏按钮弹出下拉菜单的功能.先上图: 2.代码实现 首先是activity_main.xml布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width=&qu

初学ToggleButton 点击按钮,更换按钮背景图片;再次点击,恢复之前背景图

上方的图标,R.drawable.register_checked  是选中图片 下方的图标,   R.drawable.register_unchecked 是未选中图片 默认是上方的选中效果.点击按钮,取消选中:再次点击按钮,再次选中. 其实这是两张图片.关键在于图片必须跟着按钮改变. 一开始想用Button实现,纠结了很久,只有按上去和松开的效果,并没有点击后变背景图片的效果. 后来百度了很久,找到可以 用ToggleButton实现我想要的效果,在这里与大家分享. XML: androi