Mutual invocation of Android and HTML

Source: Internet
Author: User

1. Write HTML and Android layouts

<Button Android:id="@+id/btn"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/> <WebView Android:id="@+id/webview"Android:layout_width="match_parent"Android:layout_height="wrap_content"> </WebView>
<!doctype html>

2. Writing activity

    Private WebView WebView;    Private Webappinterface Webappinterface;

  

 classwebappinterface{PrivateContext Mcontext;  PublicWebappinterface (Context context) { This. mcontext=context; } @JavascriptInterface Public voidSayHello (String name) {Toast.maketext (Mcontext,name,toast.length_short). Show (); }         Public voidshowname (final String name) {Runonuithread (NewRunnable () {@Override Public voidrun () {Webview.loadurl ("javascript:showname ('"+name+"')");        }            }); }    }
webview=Findviewbyid (R.id.webview); String URL="Http://a.st";        Webview.loadurl (URL); Webview.getsettings (). setjavascriptenabled (true); Webappinterface=NewWebappinterface ( This); Webview.addjavascriptinterface (Webappinterface,"app"); Button Button=Findviewbyid (R.ID.BTN); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Webappinterface.showname ("Wirhiutrhi"); }        });

To look at the setting configuration of the WebView control to better set

Can be implemented to call each other, can do hybird pull

Mutual invocation of Android and HTML

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.