PopupWindow显示时未调用onBackPressed

Jor*_*ens 3 android android-fragments android-popupwindow

嘿伙计们,

所以目前我正在使用PopupWindow来显示应用程序内浏览器.然而,当按下后退按钮时它什么都不做.我在另一个片段中使用PopupWindow,然后我使用一个语句在我的FragmentActivity中设置PopupWindow,然后当我按下我的后退按钮时,它应该检查PopupWindow是否被设置并且不关闭它.但它甚至没有通过onBackPressed运行.

片段中的PopupWindow:

- >是指出确保FragmentActivity获取PopupWindow的行.

// Use webview for icons and website link.
public void inAppBrowser(String url){
    mCursor.moveToFirst();
    // Inflate View
    LayoutInflater layoutInflater = (LayoutInflater) ((MainActivity) MainActivity.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View inflatedView = layoutInflater.inflate(R.layout.browser_window, null, false);

    // Control View Childs.
    LinearLayout header = (LinearLayout) inflatedView.findViewById(R.id.filter_header);
    header.setBackgroundColor(Color.parseColor(color));
    Button cancel = (Button) inflatedView.findViewById(R.id.cancel);
    Button done = (Button) inflatedView.findViewById(R.id.done);

    // Set PopupWindow position.
    Display display = ((MainActivity) MainActivity.getContext()).getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    // Control PopupWindow.
    final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, true);
    popWindow.setAnimationStyle(android.R.anim.fade_in);
    popWindow.setFocusable(true);
    popWindow.setOutsideTouchable(true);

    popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 150);
    --> MainActivity.setPopupWindow(popWindow);

    // Control WebView
    WebView myWebView = (WebView) inflatedView.findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClientAdapter());
    myWebView.clearCache(true);
    myWebView.clearHistory();
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    MainActivity.setWebView(myWebView);
    if (url != null) {
        if (url.contains("http://") || url.contains("https://")) {

        } else {
            url = "http://" + url;
        }
        myWebView.loadUrl(url);
    } else {
        popWindow.dismiss();
        MainActivity.setPopupWindow(null);
        MainActivity.setWebView(null);
    }

    cancel.setVisibility(View.INVISIBLE);
    done.setText("Close");

    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popWindow.dismiss();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我的onBackPressed代码:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    //check if popupwindow is open
    Log.e(TAG, "Check if it runs through this section");
    if (popWindow != null) {
        if (myWebView != null && myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            popWindow.dismiss();
            popWindow = null;
            myWebView = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

暂时忽略WebView.这可能是未来的问题,但我希望PopupWindow先关闭.任何帮助表示赞赏.

Sim*_*mas 10

让你的PopupWindow无法集中精力:

final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, false);
Run Code Online (Sandbox Code Playgroud)

同时删除这条多余的行:

popWindow.setFocusable(true);
Run Code Online (Sandbox Code Playgroud)

  • 但现在键盘不会在点击 EditText 时显示): (2认同)