我想通过相同的按钮关闭弹出按钮但是当我再次点击按钮时,它重新打开窗口而不是关闭它,当我在窗口外面任何地方点击时也应该关闭窗口,任何人都可以帮助我吗?
这是我的代码,
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 :(得分:0)
制作一个类似showDialog的布尔标志。 showDialog = false第一次
if(!showDialog){
display popup
showDialog=true
}else{
popup.dismiss();
showDialog=false;
}
答案 1 :(得分:0)
尝试将变量isShowing
设为全局静态变量,并将其置于onClickListener
之外,因为在您的代码中,每次单击View
时,它都会再次重新初始化值false
所以你永远不会做所需的行为
public class ClassName .....{
boolean isShowing = false;
...
...
...
ivmainmenu.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(View v) {
if(isShowing)
{
PopupWindow popupWindow = null;
popupWindow.dismiss();
isShowing=false;
}
else
{
isShowing=true;
...
...
...
答案 2 :(得分:0)
//添加isShowing = true;你的其他部分代码中的陈述
ivmainmenu.setOnClickListener(new OnClickListener(){
@SuppressWarnings("null")
@Override
public void onClick(View v) {
boolean isShowing = false;
if(isShowing)
{
PopupWindow popupWindow = null;
popupWindow.dismiss();
isShowing=false;
}
else
{
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(ivmainmenu, 0,14);
popupView.setPadding(0, 0, 0, 10);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOutsideTouchable(true);
isShowing=true; //This statement will solve your problem.
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();
}
});;
}
}
});