How to load an URL inside a WebView using Android Kotlin?

*爱你&永不变心* 提交于 2020-05-26 10:35:19

问题


I am new to both Android and Kotlin.

Anyone can help me to load an URL in a webView?

I just want to load the URL and view the webpage when the app is open.


回答1:


1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kotlinwebview.MainActivity">


<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

2.MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        webView = findViewById(R.id.webview)
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        webView.loadUrl("https://www.google.co.in/")
    }
}



回答2:


Add this to your activity:

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_webview)

    val mWebView = findViewById<WebView>(R.id.webview)

    val webSettings = mWebView.settings
    webSettings.javaScriptEnabled = true
    mWebView.loadUrl(getString(R.string.website_url))
    mWebView.webViewClient = HelloWebViewClient()
    WebView.setWebContentsDebuggingEnabled(false)

}

And add the following to the HelloWebViewClient() class in order to allow back button navigation and to outbound external links:

    override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        if (Uri.parse(url).host == getString(R.string.website_domain)) {
            return false
        }
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        startActivity(intent)
        return true
    }

    override fun onPageFinished(view: WebView, url: String) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url)
    }

}

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
        webview.goBack()
        return true
    }
    return super.onKeyDown(keyCode, event)
}

Add strings defining website_domain (https://stackoverflow.com/) and website_host (stackoverflow.com).

If you're dealing with difficulties in order to achieve what you're trying, try this full sample and just follow the instructions so as you just need to edit the URL and the clean domain name of your website.




回答3:


Use Android Kotlin Extension plugin. If you are using Android Studio 3.0 and above, don't worry about that. That already installed on your Project.

Use the id of WebView in layout and call loadUrl() method.

Android WebView Example - Kotlin & Java

Here id of WebView : webview

webview.loadUrl("https://www.androidride.com")

don't forget to add

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

If your third-party browser open with any URL that loaded in WebView, then provide WebViewClient.

webview.webViewClient = WebViewClient()

If you want to play youtube videos in your WebView, then you must enable JavaScript in WebView.

webview.settings.javaScriptEnabled = true

If you are a beginner in Android app development, then this tutorial will help you to clear almost all your doubts about WebView.

click here



来源:https://stackoverflow.com/questions/47872078/how-to-load-an-url-inside-a-webview-using-android-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!