JavaScript tune Java

Source: Internet
Author: User

1. Mapping Java objects to JavaScript objects

Mainactivity.java

Package Com.example.jsdemo;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import    Android.webkit.websettings;import Android.webkit.webview;public class Mainactivity extends Appcompatactivity {    Private WebView Wview;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Wview = (WebView) Findviewbyid (R.id.wview);        Wview.loadurl ("file:///android_asset/demo1.html");        WebSettings websettings = Wview.getsettings ();        ① set WebView allows the invocation of JS websettings.setjavascriptenabled (TRUE);        Websettings.setdefaulttextencodingname ("UTF-8");    ② Settings Support JS call Java wview.addjavascriptinterface (new Androidandjsinterface (), "Android"); } class androidandjsinterface{@JavascriptInterface public void Showtoast () {Toast.maketext (MainA        Ctivity.this, "I was called by JS", Toast.length_short). Show (); }    }}

Note: Two ways to resolve the Webview.addjavascriptinterface interface that do not work

① changed to 16 for version

② the JavaScript interface class method plus @javascriptinterface annotations

2.JavaScript calling Java Object Example

Demo1.html

<input type= "button" value= "click on Android is called" onclick= "window. Android.showtoast () "/>

JavaScript tune Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.