在Android应用程序的webview中加载URL时启动屏幕

时间:2012-03-06 18:05:31

标签: android android-activity webview screen splash

我有一个应用程序,它有2个活动,第一个启动第二个将URL加载到webview中。

它有效,但是当网址加载时,webview显示为空...然后我想制作一个启动画面或类似的东西,在网址加载时显示它,我在新的活动中做到了,但我不知道如何在网址加载时关闭第三个活动...请任何人帮助我吗?

这是我的代码......谢谢!

public class Visor extends  Activity {

    WebView mWebView;
    int Result;

    @Override
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visor);
        Bundle extras=getIntent().getExtras();
        String s= extras.getString("url");

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);

        mWebView.loadUrl(s);
        mWebView.setWebViewClient(new VisorClient());
        mWebView.getSettings().setBuiltInZoomControls(true);

        }

    private class VisorClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                lanzarIntro();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl(url);
            }
     }

    public void lanzarIntro(){
        Intent i=new Intent (this, Intro.class);

        startActivity(i);


    }



}

3 个答案:

答案 0 :(得分:61)

我是通过最初显示一个ImageView来实现的,然后一旦加载了WebView,就像这样交换它们的可见性

        WebView wv = (WebView) findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient() {

            ...

            @Override
            public void onPageFinished(WebView view, String url) {
                //hide loading image
                findViewById(R.id.imageLoading1).setVisibility(View.GONE);
                //show webview
                findViewById(R.id.webView1).setVisibility(View.VISIBLE);
            }


        });     
        wv.loadUrl("http://yoururlhere.com");

我的xml布局看起来像这样

    <ImageView android:id="@+id/imageLoading1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="visible"
        android:src="@drawable/vert_loading"
        />
    <WebView android:id="@+id/webView1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="gone"
        />

答案 1 :(得分:14)

我有一项活动。 1个xml文件和1个java类。在xml文件中我有:

  1. web视图
  2. ImageView,我的应用程序的徽标,
  3. ProgressBar和
  4. TextView,应用版本。
  5. main.xml代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
        a:layout_width="fill_parent"
        a:layout_height="fill_parent"
        a:background="#aaaaaa"
        a:orientation="vertical" >
    
    <WebView
        a:id="@+id/webView1"
        a:layout_width="fill_parent"
        a:layout_height="fill_parent" />
    
    <ImageView
        a:id="@+id/imageView1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentTop="true"
        a:layout_centerHorizontal="true"
        a:layout_marginTop="46dp"
        a:src="@drawable/logo" />
    
    <ProgressBar
        a:id="@+id/progressBar1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_below="@+id/imageView1"
        a:layout_centerHorizontal="true" />
    
    <TextView
        a:id="@+id/textView1"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentBottom="true"
        a:layout_alignParentRight="true"
        a:layout_marginBottom="13dp"
        a:layout_marginRight="13dp"
        a:text="version 1.0"
        a:textAppearance="?android:attr/textAppearanceSmall"
        a:textColor="#444444" />
    
    </RelativeLayout>
    

    NovcanikActivity.java代码:

    package zm.Nocanik;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.DownloadListener;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    
    public class NovcanikActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        WebSettings websettings = webview.getSettings();
    
        websettings.setJavaScriptEnabled(true);
        websettings.setSaveFormData(false);
        websettings.setSavePassword(false);
    
        webview.loadUrl("http://m.novcanik.net/?appvers=1.0");
        webview.setHorizontalScrollBarEnabled(false);
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.setBackgroundColor(128);
    
        webview.setWebViewClient(new NovcanikWebViewClient());
    
        webview.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
    
            }
        });
    
    
    
    
    }
    
    public void visible(){
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        ImageView logo = (ImageView) findViewById(R.id.imageView1);
    
        ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
    
        TextView version = (TextView) findViewById(R.id.textView1);
    
        webview.setVisibility(10);
    
        logo.setVisibility(0);
    
        bar.setVisibility(0);
    
        version.setVisibility(0);
    
    }
    
    public void unvisible(){
    
        WebView webview = (WebView) findViewById(R.id.webView1);
    
        ImageView logo = (ImageView) findViewById(R.id.imageView1);
    
        ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
    
        TextView version = (TextView) findViewById(R.id.textView1);
    
        webview.setVisibility(0);
    
        logo.setVisibility(10);
    
        bar.setVisibility(10);
    
        version.setVisibility(10);
    
    }
    
    
    private class NovcanikWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url){
            webview.loadUrl(url);
            return true;
        }
    
    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        // TODO Auto-generated method stub
        view.loadUrl("file:///android_asset/noconnection.html");
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
        visible();
    
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
    
        unvisible();
    
    }
    
    }
    
    }
    

    很抱歉没有说明。如果需要描述,我将详细描述整个代码。

答案 2 :(得分:0)

只需将mWebView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));用于您的mWebView