Android: Interaction Between WebView and Javascript (mutual parameter calls and value passing), androidwebview

Source: Internet
Author: User

Android: Interaction Between WebView and Javascript (mutual parameter calls and value passing), androidwebview

In Android, you can use WebView to load web pages. At the same time, the java code of Android can be called with the javascript code on the web page.


(1) Android:

Layout code:

<LinearLayout xmlns: android = "" xmlns: tools = "" android: layout_width = "match_parent" android: layout_height = "match_parent" android: focusable = "true" android: focusableInTouchMode = "true" android: orientation = "vertical" android: padding = "8dp" tools: context = ". mainActivity "> <LinearLayout android: layout_width =" match_parent "android: layout_height =" wrap_content "> <EditText android: id =" @ + id/input_et "android: layout_width = "0dp" android: layout_height = "wrap_content" android: singleLine = "true" android: layout_weight = "1" android: hint = "enter the information"/> <Button android: text = "Java call JS" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "sendInfoToJs"/> </LinearLayout> <WebView android: id = "@ + id/webView" android: layout_width = "match_parent" android: layout_height = "match_parent"/> </LinearLayout>
Activity Code:
/*** Android WebView interacts with Javascript. */Public class MainActivity extends ActionBarActivity {private WebView webView; @ SuppressLint ({"SetJavaScriptEnabled", "AddJavascriptInterface"}) @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); webView = (WebView) findViewById (R. id. webView); webView. setVerticalScrollbarOverlay (true); // sets WebView to support JavaScript WebView. getSettings (). setJavaScriptEnabled (true); String url = "http: //"; webView. loadUrl (url); // call the local java method webView in js. addJavascriptInterface (new JsInterface (this), "AndroidWebView"); // Add a client to support webView. setWebChromeClient (new WebChromeClient ();} private class JsInterface {private Context mContext; public JsInterface (Context context) {this. mContext = con Text;} // call window. AndroidWebView. showInfoFromJs (name) in js to trigger this method. @ JavascriptInterface public void showInfoFromJs (String name) {Toast. makeText (mContext, name, Toast. LENGTH_SHORT ). show () ;}// call the js Code public void sendInfoToJs (View view) {String msg = (EditText) findViewById (R. id. input_et )). getText (). toString (); // call the function showInfoFromJava (msg) webView in js. loadUrl ("javascript: showInfoFromJava ('" + msg + "')");}}
(2) webpage code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" ""> 

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: 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.