我希望用同一个PopupWindow
关闭Button
,但是当我再次点击Button
时,它会重新打开PopupWindow
而不是关闭它,也应该当我点击PopupWindow
以外的任何地方时,关闭PopupWindow
,有人可以帮助我吗?
这是我的代码,
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();
}
});
}
}
});
当我尝试这段代码时,当我再次点击同一个按钮时出现错误,这是我得到的logcat错误,
任何人都可以帮忙解决这个错误吗?谢谢。
答案 0 :(得分:1)
按照我的方式:
int x=1;
ivmainmenu.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(View v) {
if((x%2)!=0){
//Dismiss PopUp
}else{
//Show PopUp
}
x++;
}
});
并且还全局定义了您的popupWindow
并制作了
popupWindow.setOutsideTouchable(false);
popupWindow.setCanceledOnTouchOutside(false)
<强>更新强>
PopupWindow popupWindow = null;
Boolean isShowing=false;
ivmainmenu.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(View v) {
if(isShowing)
{
if(popupWindow != null && popupWindow.isShowing()){
popupWindow.dismiss();
}
isShowing=false;
}
else
{
isShowing=true;
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
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(false);
popupWindow.setCanceledOnTouchOutside(false)
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);
popupWindow.dismiss();
startActivity(intent);
isShowing=false;
}
});
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);
popupWindow.dismiss();
startActivity(intent);
isShowing=false;
}
});
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);
popupWindow.dismiss();
startActivity(intent);
isShowing=false;
}
});
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);
popupWindow.dismiss();
startActivity(intent);
isShowing=false;
}
});
}
}
});
给我反馈
答案 1 :(得分:0)
更改onClick()
方法如下:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(popupWindow != null && popupWindow.isShowing()){
popupWindow.dismiss();
}
}
答案 2 :(得分:0)
如果您想要使用相同的按钮关闭弹出窗口并单击外部的任何位置,则必须实现
设置可聚焦功能可确保弹出可以抓取外部触摸事件,因为它还会捕获菜单项或按钮上的点击,它确保弹出窗口如果已经显示则不会再次启动。
要将上述逻辑放在代码中,您必须执行以下操作。
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;
}
});