关闭Android弹出窗口并按回键
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关闭Android弹出窗口并按回键相关的知识,希望对你有一定的参考价值。
我创建了一个android应用程序,并在其中创建了一个弹出屏幕。但是当我按下“后退”按钮时,弹出窗口并没有关闭。
我已经尝试过使用onBackPressed()。它不起作用。
有人可以告诉我该怎么做。
问候,
Shankar
答案
您需要做的是在初始化PopupWindow之后在其上调用setBackgroundDrawable。类似于:
myPopup.setBackgroundDrawable(new BitmapDrawable());
另一答案
最近我使用ListPopupWindow(android.support.v7.internal.widget.ListPopupWindow
),并且在我打电话时后退按钮开始起作用
popupWindow.setModal(true);
无论我在setBackgroundDrawable
方法中设置的是什么,如此处所假定的其他解决方案。
另一答案
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_window_country_list, null);
countryPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
countryPopup.setBackgroundDrawable (new BitmapDrawable());
countryPopup.setFocusable(true); //Make Here True For back press dismiss
countryPopup.setOutsideTouchable(true);
countryPopup.setTouchInterceptor(new OnTouchListener()
public boolean onTouch(View v, MotionEvent event)
if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
countryPopup.dismiss();
return true;
return false;
);
另一答案
100%弹出窗口在按下后将消失。用下面的代码替换您的Popup代码
public void popup()
View popUpView_pur = getActivity().getLayoutInflater().inflate(R.layout.popup, null);
PopupWindow popuplayout_pur = new PopupWindow(popUpView_pur, -1, -1, true);
popuplayout_pur.setBackgroundDrawable(new BitmapDrawable());
popuplayout_pur.setOutsideTouchable(true);
popuplayout_pur.showAtLocation(popUpView_pur, 17, 0, 0);
(或)
public void popup()
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popuplayout, null, false);
PopupWindow pw = new PopupWindow(getActivity());
pw.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
pw.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
pw.setTouchable(true);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setContentView(popupView);
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
另一答案
//here "popUp" is ref of PopupWindow
popUp.setBackgroundDrawable(new BitmapDrawable());// it is most important peace of code
// For Key Listeners
View v = popUp.getContentView();
//Here assigning the key Listners
v.setOnKeyListener(this);
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
if(keyCode == KeyEvent.KEYCODE_BACK) popUp.dismiss();
return false;
//Implements the KeyListener
//and be careful we should implement "OnKeyListener"`
我希望它有用(我是新用户)
另一答案
以下代码运行正常。因此,请在您的活动中覆盖以下功能
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
//Changes 'back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
if(!popUpHelper.isPopupShowing())
onBackPressed();
else
popUpHelper.dismiss();
return super.onKeyDown(keyCode, event);
class PopupHelper
PopupWindow popupWindowAttachment;
public void initAndShow(Activity context,int mToolbarHeight)
View layout = activity.getLayoutInflater().inflate(
R.layout.activity_chat_attachment_popup, null);
//create and show and Make sure popup window focusable should be false
popupWindowAttachment = new PopupWindow(layout,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindowAttachment.showAsDropDown(layout, 0, mToolbarHeight
+ (mToolbarHeight / 2) - 5);
public void dismiss()
if (isPopupShowing())
popupWindowAttachment.dismiss();
public boolean isPopupShowing()
return popupWindowAttachment==null?false:popupWindowAttachment
.isShowing();
另一答案
当您按下返回按钮时,弹出窗口通常会关闭。如果您需要在弹出窗口关闭时处理某些事情,则可以使用popupWindow.setOnDismissListener
注:-我在科特林工作。它对我有用
另一答案
popup.setBackgroundDrawable(new BitmapDrawable());
popup.setOnDismissListener(new PopupWindow.OnDismissListener()
@Override
public void onDismiss()
//do your code here
);
以上是关于关闭Android弹出窗口并按回键的主要内容,如果未能解决你的问题,请参考以下文章