やること
htmlファイルにあるボタンを押下すると、Android側のメソッドを走らせる
呼び出す関数
connectOffline(){}
error_0.html [html側]
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>エラー画面</title>
<meta name="description" content="エラー画面">
<!-- style -->
<link rel="stylesheet" href="destyle.css">
<link rel="stylesheet" href="style.css">
</head>
<body class="error">
<div class="wrap">
<!-- ====== header ====== -->
<header class="header">
</header>
<!-- ====== main ====== -->
<main class="main">
<!-- ====== error ====== -->
<section class="section--error">
<h1 class="error--tit">通信に失敗しました。</h1>
</section>
<!-- ====== reload ====== -->
<section class="section--reload">
<button class="btn--reload" type="button" onclick="MyFunction()">再度読み込み
</button>
<script>
function MyFunction() {
alert("abcdefg");
appJsInterface.connectOffline()
}
</script>
</section>
</main>
</div><!-- wrap -->
</body>
</html>
WebAppInterface.kt [連携用(どこに書いても良いよ)]
package com.example.bluetoothkotlindemo
import android.webkit.JavascriptInterface
class WebAppInterface {
@JavascriptInterface
fun connectOffline() {
println("connectOffline")
MainActivity().goBack()
}
}
MainActivity.kt
true.also { webview.settings.javaScriptEnabled = it }
webview.addJavascriptInterface(WebAppInterface(), "appJsInterface")
//設置したhtmlファイルへのパス
web_view.loadUrl("file:///android_asset/error_0.html")