popupwindow通过相同的按钮关闭问题 [英] popupwindow closing issue by same button
问题描述
我想用同样的按钮
关闭 PopupWindow
,但是当我点击按钮
再次,它重新打开的 PopupWindow
,而不是关闭它,也应该关闭 PopupWindow
当我点击以外的 PopupWindow
任何地方,任何人都可以帮我吗?
I wanna close the PopupWindow
by the same Button
, But when I click on the Button
again, it reopen the PopupWindow
instead of closing it, and also should close the PopupWindow
when i click outside of the PopupWindow
any where, can anyone help me?
这是我的code,
ivmainmenu.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(View v) {
if(isShowing)
{
PopupWindow popupWindow = null;
popupWindow.dismiss();
isShowing=false;
}
else
{
isShowing=true;
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(ivmainmenu, 0,14);
popupView.setPadding(0, 0, 0, 10);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork);
TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout);
TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices);
TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact);
Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/arboriaboldregular.ttf");
tvpopupwork.setTypeface(typeFace2);
tvpopupabout.setTypeface(typeFace2);
tvpopupservices.setTypeface(typeFace2);
tvpopupcontact.setTypeface(typeFace2);
tvpopupwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Home.this,Ourwork.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
popupWindow.dismiss();
}
});
tvpopupabout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Home.this,Aboutus.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
popupWindow.dismiss();
}
});
tvpopupservices.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Home.this,Services.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
popupWindow.dismiss();
}
});
tvpopupcontact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Home.this,Contact.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
popupWindow.dismiss();
}
});
}
}
});
当我尝试这样做code,当我在同一个按钮,再次点击,我得到一个错误,这是我的logcat错误,我得到的,
When I tried this code I am getting an error when I click again on the same button, this is my logcat error which i get,
任何人都可以帮助解决这个错误?谢谢你。
can anyone help to solve this error? thank you.
推荐答案
如果你想用相同的按钮和点击以外的任何地方关闭弹出那么你将不得不实施
If you want to close the popup with same button and on clicking anywhere outside then you will have to implement
- 触摸拦截并关闭弹出窗口时,动作的 MotionEvent.ACTION_OUTSIDE 。
- 设置弹出窗口为可聚焦使用的 setFocusable(真)
- The touch interceptor and dismiss the popup when action is MotionEvent.ACTION_OUTSIDE.
- Set the popup to be focusable using setFocusable(true)
设置聚焦的保证弹出的可以在外面触摸事件抢并因为它也将捕获的菜单项或按钮点击,它确保弹出窗口不会再次启动,如果它已经显示。
Setting focusable ensures that popup can grab outside touch events and since it will also capture the click on the menuitem or a button, It ensures that popup is not launched again if it is already showing.
要,放于code以上的逻辑,你必须做到以下几点。
To, put the above logic in code, you have to do the following.
final MenuItem popupMenu= menu.findItem(R.id.action_open_popup);
popupMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (window == null) {
View contentView = getLayoutInflater(null).inflate(R.layout.popup_menu, null);
window = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(new BitmapDrawable(getResources(), ""));
window.setOutsideTouchable(true);
window.setFocusable(true);
window.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
window.dismiss();
return true;
}
return false;
}
});
}
//anchor as the menuitem this is in fragment so.
window.showAsDropDown(getActivity().findViewById(R.id.action_open_popup));
return true;
}
});
这篇关于popupwindow通过相同的按钮关闭问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!