gpt4 book ai didi

android - 显示 PopupWindow 时未调用 onBackPressed

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:34 24 4
gpt4 key购买 nike

嘿伙计们,

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

fragment 中的弹出窗口:

--> 是我指出确保 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();
}
});
}

我的 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;
}
}
}

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

最佳答案

使您的 PopupWindow 不可聚焦:

final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, false);

同时删除多余的这一行:

popWindow.setFocusable(true);

关于android - 显示 PopupWindow 时未调用 onBackPressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com