Android 通过后退按钮关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21898723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dismiss the popup window by back button
提问by akky777
I want to Dismiss the popup window by clicking outside of the popup window or by the back button, but when click on the back button my application exit's, instead of exiting the application I want to close the popup window.
我想通过单击弹出窗口外部或后退按钮来关闭弹出窗口,但是当单击后退按钮时,我的应用程序退出,而不是退出应用程序,我想关闭弹出窗口。
here is my code,
这是我的代码,
ivmainmenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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.showAsDropDown(ivmainmenu);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(false);
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);
}
});
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);
}
});
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);
}
});
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);
}
});
ivmainmenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
});
Its gives me the result what I want but when I closes the menu the it does not opens again, I want to open it again so what should I do? thank you.
它给了我想要的结果,但是当我关闭菜单时它不会再次打开,我想再次打开它,我该怎么办?谢谢你。
回答by Chirag Ghori
Replace
代替
popupWindow.setOutsideTouchable(false);
with this
有了这个
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
回答by Gopal Gopi
Maintain global reference for PopUpWindow
and override onBackPressed()
...
维护全局引用PopUpWindow
并覆盖onBackPressed()
...
@Override
public void onBackPressed() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
super.onBackPressed();
}
}
To dismiss by the same Button
...
被同样的解雇Button
...
ivmainmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
} else {
// show pop up now
}
}
});
回答by Jitesh Upadhyay
please write onBackPressed(
) and have following code
请写onBackPressed(
)并有以下代码
if(popup!=null){
//dismiss the popup
popup.dismiss();
//make popup null again
popup=null;
}
回答by M D
Try this way: Implement onBackPressed()
and add
试试这种方式:实现onBackPressed()
并添加
if(popup!=null) {
popup.dismiss();
popup=null;
}
And set your PopWindow
with below:
并在PopWindow
下面设置您的:
popup.setOutsideTouchable(true);
回答by Behnam
You can override onBackPressed() callback in your code and check to see if your pop-up is already showing(then dismiss it), else you call super to get normal behavior.
您可以在代码中覆盖 onBackPressed() 回调并检查您的弹出窗口是否已经显示(然后关闭它),否则您调用 super 以获得正常行为。
回答by Hariharan
Try this..
尝试这个..
Use PopupWindow popupWindow
as Global variable
使用PopupWindow popupWindow
为全局变量
use popup.setOutsideTouchable(true);
用 popup.setOutsideTouchable(true);
@Override
public void onBackPressed() {
if (popupWindow != null) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
} else {
finish();
}
}