gpt4 book ai didi

java - 加载 Webview 后进度对话框不会关闭

转载 作者:行者123 更新时间:2023-12-02 11:05:09 24 4
gpt4 key购买 nike

加载 Webview 后进度对话框不会关闭,而是在加载 Webview 后继续加载。

public class MainActivity extends AppCompatActivity {

private WebView webView;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");

webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://kkgupta.mmiswebtech.com");



webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}

if (webView.canGoBack()) {
webView.goBack();
}

webView.loadUrl("file:///android_asset/error.html");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});

alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}

class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);
return true;

}
}





@Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}

最佳答案

您需要将 onPageFinished() 添加到 MyWebClient 中,并在其中调用 ProgressDialog 上的 dismiss():

public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}

关于java - 加载 Webview 后进度对话框不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51030006/

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