Interaction between Android WebView and html, and androidwebview

Source: Internet
Author: User

Interaction between Android WebView and html, and androidwebview

We hope to learn about android in uppercase and make progress together. Support Group 102063643 android technology development

In the process of application, more and more applications use webView to display the interface, many of which interact with js.

I wrote a Demo. Let's take a look at it and the project structure.


The code for index.html is as follows:

<Html> 


This is the MainActivity code.

Public class MainActivity extends Activity {private WebView mWebView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. changButton ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// call the JS method and pass the mWebView parameter. loadUrl ("javascript: invokedByJava ('Hello World ')");}}); MWebView = (WebView) findViewById (R. id. webView); mWebView. setScrollBarStyle (View. SCROLLBARS_INSIDE_OVERLAY); mWebView. getSettings (). setBuiltInZoomControls (true); mWebView. getSettings (). setJavaScriptEnabled (true);/* WebView uses the browser provided by the system to process page jumps by default. To redirect the page to the current WebView, rewrite WebViewClient. However, when you press the BACK key, the previous page is not returned, but the Activity is exited. Override the onKeyDown () method to solve this problem. */MWebView. setWebViewClient (new WebViewClient () {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); // use the current WebView to process the jump return true; // true indicates that the event is processed here and no broadcast is required.} @ Override public void onPageStarted (WebView view, string url, Bitmap favicon) {// callback when page Jump exists} @ Override public void onPageFinished (WebView view, String url) {// callback after page Jump ends} @ Override public void OnReceivedError (WebView view, int errorCode, String description, String failingUrl) {Toast. makeText (MainActivity. this, "Oh no! "+ Description, Toast. LENGTH_SHORT ). show () ;}});/* call the WebChromeClient method when the WebView content affects the UI */mWebView. setWebChromeClient (new WebChromeClient () {/*** handle JavaScript Alert events */@ Override public boolean onJsAlert (WebView view, String url, String message, final JsResult result) {// Replace new AlertDialog with the Android component. builder (MainActivity. this ). setTitle ("JS prompt "). setMessage (message ). setPositiveButton (android. R. string. OK, new AlertDialog. onClickListener () {public void onClick (DialogInterface dialog, int which) {result. confirm ();}}). setCancelable (false ). create (). show (); return true ;}});/* bind a Java object to WebView, so that JS can communicate with Java (JS accesses the Java method) the first parameter is a custom class object, which is mapped to a JS object. The second parameter is the JS alias call example of the first parameter: mWebView. loadUrl ("javascript: window. stub. jsMethod ('param') "); */mWebView. addJavascriptInterface (new JsToJava (), "stub "); Final EditText mEditText = (EditText) findViewById (R. id. urlEditText); findViewById (R. id. web_view_search ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {String url = mEditText. getText (). toString (); if (url = null | "". equals (url) {Toast. makeText (MainActivity. this, "Enter URL", Toast. LENGTH_SHORT ). show ();} else {if (! Url. startsWith ("http :")&&! Url. startsWith ("file:") {url = "http: //" + url;} mWebView. loadUrl (url) ;}}); // default page mWebView. loadUrl ("file: // android_asset/index.html") ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// process the WebView jump and return if (keyCode = KeyEvent. KEYCODE_BACK) & mWebView. canGoBack () {mWebView. goBack (); return true;} return super. onKeyDown (keyCode, event);} private class JsToJava {@ JavascriptInterface public void jsMethod (String paramFromJS) {Log. I ("CDH", paramFromJS );}}}


Click to call JS to change the page content


Click the "Call alert" button to capture JS alert in Android and replace it with the Android component (AlertDialog ).


Click the "Call java method" button to call and PASS Parameters to methods in Java in JS.


Finally, click Enter URL to open the webpage.


Click to download source code




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.