我有一个带有webview的Android应用程序,我在操作栏中添加了进度条,仅在第一页加载时显示。但是当我在webview中点击每个链接时,我想在操作栏上显示进度条。有人帮帮我???
这是我的main.java代码
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {
Context mContext=Main.this;
private WebView mWebview ;
SharedPreferences appPreferences;
boolean isAppInstalled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Progress bar */
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
setProgressBarIndeterminateVisibility(true);
setProgressBarVisibility(true);
/* Progress bar */
/* home Screen Shortcut */
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled",false);
if(isAppInstalled==false){
/**
* create short code
*/
Intent shortcutIntent = new Intent(getApplicationContext(),Main.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "T2Lead");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
/**
* Make preference true
*/
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.commit();
}
/* home Screen Shortcut */
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview = new WebView(this);
mWebview.setInitialScale(1);
mWebview.getSettings().setLoadWithOverviewMode(true);
mWebview.getSettings().setUseWideViewPort(true);
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setBuiltInZoomControls(true);
final Activity activity = this;
/** Showing Indeterminate progress bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(true);
mWebview.setWebViewClient(new WebViewClient() {
/*Progress Bar on title Bar */
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/** This prevents the loading of pages in system browser */
return false;
}
/** Callback method, executed when the page is completely loaded */
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/*
Toast.makeText(getBaseContext(),
"",
Toast.LENGTH_SHORT).show();
/** Hiding Indeterminate Progress Bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(false);
/*Progress Bar on title Bar */
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview );
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/* Back Navigation */
@Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
mWebview.reload();
return true;
}
else if(item.getItemId()==R.id.item2){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
使用setWebChromeClient()
而非setWebViewClient()
这样
webview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress >=100){
activity.setProgressBarIndeterminateVisibility(false);
}else{
activity.setProgressBarIndeterminateVisibility(true);
}
}
});
答案 1 :(得分:0)
答案 2 :(得分:0)
我创建了一个带有进度条的可重复使用的webview,您可以找到它here
1)创建一个布局xml文件:“view_progress_webview.xml”,其中包含一个进度条和一个webview
<ProgressBar
android:id="@+id/webview_progressbar"
style="@style/SmallProgressBarHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:progressDrawable="@drawable/greenprogress"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2)在“style.xml”
中添加样式代码<style name="SmallProgressBarHorizontal" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/greenprogress</item>
<item name="android:minHeight">5dip</item>
<item name="android:maxHeight">5dip</item>
</style>
3)在res / drawable中添加文件“greenprogress.xml”。
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@android:id/background">
<shape>
<corners android:radius="0dip" />
<gradient
android:angle="270"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:angle="270"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="0dip" />
<gradient
android:angle="270"
android:endColor="#4e551f"
android:startColor="#bbcf4e" />
</shape>
</clip>
</item>
4)创建一个“ProgressWebView.java”类
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
public class ProgressWebView extends LinearLayout {
private Context context;
private ProgressBar progressBar;
private WebView webView;
public ProgressWebView(Context context) {
this(context, null);
}
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_progress_webview, this);
progressBar = (ProgressBar) findViewById(R.id.webview_progressbar);
webView = (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new ArticleWebChromeClient());
}
public WebView getWebView() {
return this.webView;
}
private class ArticleWebChromeClient extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100
&& progressBar.getVisibility() == ProgressBar.GONE) {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(ProgressBar.GONE);
}
}
}
}
答案 3 :(得分:0)
更简单的方法:
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
// Progress in ActionBar (if you want)
// Add to Activity.onCreate: getWindow().requestFeature(Window.FEATURE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
}
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
setProgressBarIndeterminateVisibility(false);
}
});