0

我需要显示启动画面,直到网页在 webview 中加载。

我使用以下代码。有可能吗?

public class SplashScreen extends Activity {

    protected Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash_screen);

        intent = new Intent(getApplicationContext(), MainActivity.class);

        // MainActivity.class  contains WebView

        Thread myThread = new Thread() {
            @Override
            public void run() {

                try {
                    sleep(5000);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        myThread.start();
    }
}

public class MainActivity extends Activity {

    private WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        String url = "http://google.com";
        view = (WebView)this.findViewById(R.id.webView1);

        view.clearCache(true);
        WebSettings s = view.getSettings();
        s.setJavaScriptEnabled(true);

        s.setCacheMode(WebSettings.LOAD_DEFAULT);
        s.setDomStorageEnabled(true);

        view.loadUrl(url);
    }
4

2 回答 2

1

不要为启动屏幕创建单独的活动,在 MainActivity.java 本身中创建启动屏幕布局和 webview 布局并将 webview 的可见性设置为 GONE。

在 MainActivity 打开时初始化 webView 并设置自定义 WebViewClient。在您的自定义 webViewClient 中覆盖 onPageFinished() 并在此方法中使 webview 可见并且启动屏幕布局消失。

此处相同:在启动画面完成之前加载 WebView URL

于 2017-02-12T18:15:18.607 回答
0

我的解决方案在这里

private WebView view;
private ImageView splashScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    splashScreen =   (ImageView) this.findViewById(R.id.spscreen);

    String url = "http://google.com";
    view = (WebView)this.findViewById(R.id.webView1);

    view.setWebViewClient(new WebViewClient() {            

       public void onPageFinished(WebView view, String url) {
            // do your stuff here
            splashScreen.setVisibility(View.INVISIBLE);
            view.setVisibility(View.VISIBLE);
        }
    });

      WebSettings s = view.getSettings();
    s.setJavaScriptEnabled(true);

    s.setCacheMode(WebSettings.LOAD_DEFAULT);
    s.setDomStorageEnabled(true);

    view.loadUrl(url);
}

和布局

<ImageView
    android:id="@+id/spscreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/splashscreen"
     android:visibility="visible"
    android:scaleType="fitXY"/>

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView1"
    android:visibility="invisible" />
于 2017-02-12T18:31:29.647 回答