简体   繁体   English

Android kotlin Webview 2主题启动画面

[英]Android kotlin Webview Splash Screen with 2 Theme

Sorry for my bad English I have an Android Kotlin Webview project splash screen with two theme my project is working fine But I need To Splash Screen wait to Website Load Completely And The Hide Here is My project codes: MainActivity.kt:抱歉我的英语不好我有一个 Android Kotlin Webview 项目启动画面有两个主题我的项目工作正常但我需要启动屏幕等待网站加载完全主要活动和我的项目代码隐藏这里是

    class MainActivity : Activity() {
    private var mWebView: WebView? = null

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTheme(R.style.Theme_Splash_kotlin_)
        setContentView(R.layout.activity_main)
        mWebView = findViewById(R.id.activity_main_webview)
        val webSettings = mWebView?.getSettings()
        if (webSettings != null) {
            webSettings.javaScriptEnabled = true
        }

        mWebView?.setWebViewClient(MyWebViewClient())

        // REMOTE RESOURCE
        mWebView?.loadUrl("https://fa.azdamghest.com/")

        // LOCAL RESOURCE
        // mWebView.loadUrl("file:///android_asset/index.html");
    }

    override fun onBackPressed() {
        if (mWebView!!.canGoBack()) {
            mWebView!!.goBack()
        } else {
            super.onBackPressed()
        }
    }
}

MyWebViewClient.java: MyWebViewClient.java:

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String hostname;

        // YOUR HOSTNAME
        hostname = "fa.azdamghest.com";

        Uri uri = Uri.parse(url);
        if (url.startsWith("file:") || uri.getHost() != null && uri.getHost().endsWith(hostname)) {
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

activity_main.xml: activity_main.xml:

<WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

themes.xml:主题.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Splash_kotlin_" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    
    <style name="splashScreenTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    
        <item name="android:windowBackground">@drawable/splash_image</item>

        
    </style>
    
</resources>

AndroidManifest.xml: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.splash_kotlin_">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/splashScreenTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you Very much非常感谢您

Set the visiblity to invisible:将可见性设置为不可见:

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible" />

Then put this underneath mWebView?.setWebViewClient(MyWebViewClient()) :然后把它放在mWebView?.setWebViewClient(MyWebViewClient())下面:

mWebView.webViewClient = object: WebViewClient() {
   var hasWebViewFinishedLoading: Boolean = false
   override fun onPageFinished(viewEl: WebView?, urlStr: String) {
      super.onPageFinished(viewEl, urlStr)
      if (mWebView.progress >= 100 && !hasWebViewFinishedLoading) {
          // Page finished loading.
          hasWebViewFinishedLoading = true
          // Hide the launch screen.
          yourSplashScreenEl.visibility = INVISIBLE
        
          // Show the webView.
          mWebView.visibility = VISIBLE
      }
   }
}

So when the page finishes loading for the first time, it hides the splash screen.因此,当页面第一次完成加载时,它会隐藏启动画面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM