如何使用选项卡在应用程序中放置进度条

时间:2010-12-02 20:12:55

标签: android webview progress-bar tabactivity

我有一个主java文件,它设置了一系列标签,每个标签都调用一个单独的活动,在这种情况下,每个活动都链接到一个网页。我正在尝试在选项卡活动中实现进度条,该活动将显示单击链接时的进度条,而不管当前选择了哪个选项卡。我有一个进度条,我可以用于单个活动,但我不能让它为标签工作

我将进度条定义为

    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
   setContentView(R.layout.main );
   webview = (WebView) findViewById(R.id.webview);
   webview.setWebChromeClient(new WebChromeClient() {
       public void onProgressChanged(WebView view, int progress)   
       {
        MyActivity.setProgress(progress * 100);
           if(progress == 100)

              MyActivity.setTitle(R.string.app_name);
         }
       });

在oncreate()

之后

这是主要的Java文件

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TabHost;
import android.widget.TextView;

public class UniversityofColorado extends TabActivity{

    WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        TabHost host=getTabHost();
        Resources res = getResources();

        host.addTab(host.newTabSpec("one")
                .setIndicator("",res.getDrawable(R.drawable.ic_tab_google))
                .setContent(new Intent(this, Hello.class)));

        host.addTab(host.newTabSpec("two")
                        .setIndicator("Colorado Main Site")
                        .setContent(new Intent(this, ColoradoMainSiteBrowser.class)));

        host.addTab(host.newTabSpec("three")
                        .setIndicator("CULearn")
                        .setContent(new Intent(this, CULearnBrowser.class)));

        host.addTab(host.newTabSpec("four")
                .setIndicator("CULink")
                .setContent(new Intent(this, CULinkBrowser.class)));

        host.addTab(host.newTabSpec("five")
                .setIndicator("MyCUInfo")
                .setContent(new Intent(this, MyCUInfoBrowser.class)));

        host.addTab(host.newTabSpec("six")
                .setIndicator("", res.getDrawable(R.drawable.ic_tab_map))
                .setContent(new Intent(this, CampusBrowser.class)));

        host.addTab(host.newTabSpec("Seven")
                .setIndicator("",res.getDrawable(R.drawable.ic_tab_notes))
                .setContent(new Intent(this, NotesList.class)));

        host.addTab(host.newTabSpec("eight")
                .setIndicator("", res.getDrawable(R.drawable.ic_tab_help))
                .setContent(new Intent(this, CampusBrowser.class)));
    }   

这是主java文件调用的活动之一

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CampusBrowser extends Activity {

    WebView webview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new HelloWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://amath.colorado.edu/department/Maps/bldgs.html");
    }
    private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
    view.loadUrl(url);
    return true;
    }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    webview.goBack();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
    }

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

如果你能帮我解决这个问题,我们将不胜感激

1 个答案:

答案 0 :(得分:1)

嘿,这正是我在网络视图中所做的

在webview的onCreate方法[在你的情况下它可能是你的tabActivity]这样做

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.webview_simple);
}

然后在webview客户端执行此操作

private class MyWebViewClient extends WebViewClient {

public synchronized boolean shouldOverrideUrlLoading(WebView view, String url){
}// and all ur implementation

    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        setProgressBarIndeterminateVisibility(false);
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        setProgressBarIndeterminateVisibility(true);
        super.onPageStarted(view, url, favicon);            
    }

}

希望它有所帮助。我正在覆盖webview客户端中的onPageFinished和onPageStarted。从onProgressChanged很难做到这一点。我尝试过,但我发现这很简单。