【发布时间】:2015-07-26 08:17:07
【问题描述】:
我是 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 应用程序开发的初学者。
【问题讨论】: