在加载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();
}
}
}
答案 0 :(得分:1)
您需要将onPageFinished()
添加到您的MyWebClient
中,然后在您的ProgressDialog中调用dismiss()
:
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
答案 1 :(得分:0)
使用
pd.dismiss();
您要在其中关闭对话框。
答案 2 :(得分:0)
注意:API级别26不推荐使用 ProgressDialog 。更好地使用ProgressBar
删除此代码:
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication1
{
public static class MyServiceExtensions
{
public static IServiceCollection Configure(this IServiceCollection services, Type type, Action<object> configureOptions)
{
// Static type that contains the extension method
var extMethodType = typeof(OptionsServiceCollectionExtensions);
// Find the overload for Configure<TOptions>(IServiceCollection, Action<TOptions>)
// This could be more specific to make sure that all type arguments are exactly correct.
// As it stands, this returns the correct overload but future updates to OptionsServiceCollectionExtensions
// may add additional overloads which will require this to be updated.
var genericConfigureMethodInfo = extMethodType.GetMethods()
.Where(m => m.IsGenericMethod && m.Name == "Configure")
.Select(m => new
{
Method = m,
Params = m.GetParameters(),
Args = m.GetGenericArguments() // Generic Type[] (ex [TOptions])
})
.Where(m => m.Args.Length == 1 && m.Params.Length == 2
&& m.Params[0].ParameterType == typeof(IServiceCollection))
.Select(m => m.Method)
.Single();
var method = genericConfigureMethodInfo.MakeGenericMethod(type);
// Invoke the method via reflection with our converted Action<objct> delegate
// Since this is an extension method, it is static and services is passed
// as the first parameter instead of the target object
method.Invoke(null, new object[] { services, configureOptions });
return services;
}
}
}
更改:
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;
}
}