[英]How to finish an activity when a div is clicked in webview
I want to close an activity if a <div class="close" id="closer">close</div>
is clicked如果单击
<div class="close" id="closer">close</div>
,我想关闭一个活动
<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" />
You have to add a javascript interface to your WebView.您必须将 javascript 接口添加到 WebView。
wv.addJavascriptInterface(this, "MyInterface");
Make sure you have enabled javascript:确保您已启用 javascript:
wv.getSettings().setJavaScriptEnabled(true);
Now in your activity create a function annotated with @JavascriptInterface that will finish the activity.现在在您的活动中创建一个 function 并用 @JavascriptInterface 注释来完成活动。
@JavascriptInterface
pubic void closeActivity(){
finish()
}
Now modify your HTML to call the above function:现在修改您的 HTML 以调用上述 function:
<div class="close" id="closer" onclick="MyInterface.closeActivity()">close</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.