Android webview and webpage JS call each other

Source: Internet
Author: User
Document directory
  • In Android:
  • How to Write in HTML

 

In Android:

Public voidOncreate (bundle savedinstancestate ){

Super. Oncreate (savedinstancestate );

Setcontentview (R. layout.Main);

FinalWebview web1 = (webview) findviewbyid (R. Id.Web1);

FinalButtonMybutton= (Button) findviewbyid (R. Id.Mybutton);

HandlerH=NewHandler (); // declare a parameter for passing values

Web1.getsettings (). setjavascriptenabled (True); // Determine whether this webview can use JS

Web1.addjavascriptinterface (NewA (), "to2"); // Add the Class Object to the Global Object window of Js.

Web1.loadurl ("file: // android_asset/test.html"); // This is the HTML address for loading the project and put it in the assets folder.

Webviewclient WebClient =NewWebviewclient (){

 

@ Override

Public voidOnpagefinished (webview view, string URL ){

//TodoAuto-generated method stub

Super. Onpagefinished (view, URL );

}

};

Web1.setwebviewclient (WebClient );

}

ClassA {

Public voidA (){

Here, perform the corresponding logical operation.

}

How to Write in HTML

<Title> untitled document </title>

<SCRIPT type = "text/JavaScript">

Function F2 (){

Return window. to2.a ();

}

</SCRIPT>

</Head>

 

<Body>

<Table align = "center" width = "724px" Height = "600px">

<Tr align = "center">

<TD align = "center">

<Input name = "Submit" type = "Submit" value = "jump" onclick = "Return F2 ()"/>

</TD>

</Tr>

</Table>

</Body>

</Html>

Note: QQ technology exchange group: add one if you are interested in 108614806.

 

Related Article

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.