gpt4 book ai didi

java - fragment 内的 WebView 返回按钮

转载 作者:行者123 更新时间:2023-12-01 06:04:05 25 4
gpt4 key购买 nike

在 fragment 内,我使用WebView。当点击后退按钮

时,我只是查询 2 个选项

第一个 if; url.equals("www.google.com") [完美运行]

第二个 if; !url.equals("www.google.com") [不起作用]

我在 Debug模式下工作,但我的第二个 if 不起作用,即使 url 与“google.com”不同并关闭应用程序!

Fragment.Java

@Override
public void onResume() {
super.onResume();

if (getView() == null) {
return;
}

getView().setFocusableInTouchMode(true);
getView().requestFocus();

getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {

if(url.equals("https://www.google.com")){

//normal geri

final AlertDialog.Builder builder_question1 = new AlertDialog.Builder(getContext());
builder_question1.setTitle("Are you sure to close the app?");
builder_question1.setCancelable(true);
//POSITIVE BUTTON
builder_question1.setPositiveButton("yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.exit(0);

}
});

//NEGATIVE BUTTON
builder_question1.setNegativeButton("no",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//do something...
//and dismiss
dialogInterface.dismiss();
}
});


builder_question1.create().show();



}

if(!url.equals("https://www.google.com")){
if(webView.canGoBack()){
webView.goBack();
}

}
}
return true;
}
});



}

WebViewClient

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Toast.makeText(getContext(), ""+url, Toast.LENGTH_SHORT).show();
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onLoadResource(WebView view, String url) {


super.onLoadResource(view, url);
}

@Override
public void onPageFinished(WebView view, String url) {
// Toast.makeText(getContext(), "page loaded", Toast.LENGTH_SHORT).show();
super.onPageFinished(view, url);
}

});
webView.loadUrl(url);

最佳答案

在包含 fragment 的 Activity 中应用后退按钮

关于java - fragment 内的 WebView 返回按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490383/

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