我正在尝试使用我的应用程序打开一个网页WebView
.当我打开网页时,它会显示空白屏幕一段时间,然后在我的应用程序内的浏览器中打开该页面.
有人建议我如何显示进度或摆脱加载webview期间出现的空白屏幕?
我使用以下代码:
WebView mWebView = (WebView) findViewById(R.id.mywebview); mWebView.getSettings().setJavaScriptEnabled(true); // error handling final Activity activity = this; mWebView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } }); // error handling mWebView.loadUrl(URL); mWebView.setWebViewClient(new HelloWebViewClient());
Wroclai.. 44
试试这个:
getWindow().requestFeature(Window.FEATURE_PROGRESS); WebView mWebView = (WebView) findViewById(R.id.mywebview); mWebView.getSettings().setJavaScriptEnabled(true); final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient(){ public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle("My title"); } }); mWebView.loadUrl(URL);
不推荐使用Window.FEATURE_PROGRESS (6认同)
nijas.. 21
这是我正在使用的代码:
在WebViewClient中:
webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); findViewById(R.id.progress1).setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); findViewById(R.id.progress1).setVisibility(View.GONE); } });
这是XML:
希望这可以帮助..
试试这个:
getWindow().requestFeature(Window.FEATURE_PROGRESS); WebView mWebView = (WebView) findViewById(R.id.mywebview); mWebView.getSettings().setJavaScriptEnabled(true); final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient(){ public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle("My title"); } }); mWebView.loadUrl(URL);
这是我正在使用的代码:
在WebViewClient中:
webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); findViewById(R.id.progress1).setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); findViewById(R.id.progress1).setVisibility(View.GONE); } });
这是XML:
希望这可以帮助..
你需要做这样的事情:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_PROGRESS); this.setProgressBarVisibility(true); }
然后
final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 100); } });
更新
我知道它有点太晚但我没有注意到:你不应该使用setWebViewClient()
两次.第二个呼叫取消第一个呼叫,因此您不会得到错误处理.