android - 使用后退按钮关闭 PopupWindow?

标签 android

我有一个用于显示谷歌地图的弹出窗口,但一旦它打开我似乎无法关闭它,我试图设置后退按钮以关闭它但无法调用 onBackButtonClick当我在 fragment 中时覆盖。虽然似乎没有做任何事情

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_go, container, false);
    mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);

    mRouteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View popupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);

            mPopupWindow = new PopupWindow(popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // If the PopupWindow should be focusable
            mPopupWindow.setFocusable(true);
            mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
            mPopupWindow.update(0, 0, 800, 800); //don't hardcode

后退按钮代码部分:

            mPopupWindow.getContentView().setOnKeyListener( new View.OnKeyListener(){
                @Override
                public boolean onKey( View v, int keyCode, KeyEvent event ){
                    if( keyCode == KeyEvent.KEYCODE_BACK ){
                        mPopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            } );
        }
    });

好的排序,在遵循 Deepak Singh 的 建议后,我让它工作了,尽管当我重新打开它时,由于尝试重新创建仍然存在的 View 而遇到崩溃问题。这是我的工作代码以供将来引用:

固定版本:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_go, container, false);

    mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);

    mRouteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mPopupView != null) {
                ViewGroup parent = (ViewGroup) mPopupView.getParent();
                if (parent != null)
                    parent.removeView(mPopupView);
            }
            try {
                mPopupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
            } catch (InflateException e) {
                /* map is already there, just return view as it is */
            }
            mPopupWindow = new PopupWindow(mPopupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
            mPopupWindow.update(0, 0, 800, 800); //don't hardcode

            mPopupView.findViewById(R.id.doneBtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopupWindow.dismiss();
                }
            });
        }
    });

最佳答案

在弹出窗口布局上添加一个按钮,使用popupView.findviewbyid(id)找到按钮的id,然后在这个按钮上设置clicklisener并调用mPopupWindow.dismiss();.

你也可以试试这个:

 popupView.setOutsideTouchable(true);
 popupView.setFocusable(true);

关于android - 使用后退按钮关闭 PopupWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188215/

相关文章:

Android 应用内支付动态产品列表

android - 无法实例化应用程序 由 : java. lang.ClassNotFoundException 引起:在路径上找不到类:DexPathList

android - Sdcard 文件无法在 OS 6 及更高版本中打开

android - 使用谷歌播放服务和 FusedLocationProviderClient 的位置更新

java - Android Json/Gson问题

android - 将新的 APK 上传到生产环境。对于新版本的 Android 应用程序。

android - 如何为 MenuItem 设置标签?

android - 如何在 Android 首选项 View 上设置内容描述

Java/Android 网络服务器 : multipart upload from browser to the server via HttpRequestHandler

android - 是否可以从 Android 应用程序中删除权限?