我是 Android 应用开发新手,正在学习它。
我开发了一个使用 WebView
加载网站链接的应用程序。并且运行良好。
但是当 WebView
加载页面时,我遇到了显示和隐藏进度条的问题。
我想在 WebView 加载网站的主链接和内部链接时显示一个进度条(只是中心的微调器),并在页面完全加载时隐藏进度条。
我的 Java 代码:
package in.matebook.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.labnol.org");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.labnol.org")) {
// This is my web site, so do not override; let my WebView load the page
//or we can add more allowed host in if condition
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
还有我的布局 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
此时,进度条显示,但不隐藏。
当我创建 progressbar 对象并在 Java 代码中添加以下行时,进度条不显示。
progressbar.setVisibility(View.GONE);
我也试过an other answer但是当我点击内部链接时进度条不起作用。
我想在主页加载时显示进度条,在主页完全加载时隐藏进度条。
我想在用户点击网站内部链接时再次显示进度条,同上,当页面完全加载时隐藏进度条。
请帮忙,我是 Android 应用程序开发的初学者。
最佳答案
试试这个代码...你需要 webChoromeClient 来跟踪 webview 加载进度...
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
用这段代码替换你的 webview 客户端....
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
关于java - Android中WebView加载URL时如何显示进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634963/