Android references javascript and a simple example of referencing java in javascript, androidjavascript

Source: Internet
Author: User

Android references javascript and a simple example of referencing java in javascript, androidjavascript

Simple examples of referencing javascript in android and java in javascript

In android, micro-webView can load javascript code. It is not so much javascript as loading Web pages. In fact, it is the combination of html and javascript. You can also create android applications through html and javascript, because android and javascript can call each other, the following is a simple example. You can refer to it. Welcome to join us.

// Allows JavaScript Execution
WebSettings. setJavaScriptEnabled (true );

// Add an object so that javascript can access the object,
MyWebView. addJavascriptInterface (new WebAppInterface (this ),
"MyInterfaceName ");

// Call methods in javascript in java
MyWebView. loadUrl ("javascript: myFunction ()");

For details, refer to code analysis. This simple column is easy to understand.

Package com. mlf. javascripttest; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. webkit. webChromeClient; import android. webkit. webSettings; import android. webkit. webView; import android. webkit. webViewClient; import android. webkit. jsResult; import android. widget. button; import android. widget. toast; // @ SuppressLint ("SetJavaScriptEnabled") @ SuppressLint ({"SetJavaScriptEnabled", "JavascriptInterface"}) public class MainActivity extends Activity {private WebView myWebView; private Button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); myWebView = (WebView) findViewById (R. id. javascriptWebview); button = (Button) findViewById (R. id. uttonId); WebSettings webSettings = myWebView. getSettings (); // allows JavaScript to execute webSettings. setJavaScriptEnabled (true); webSettings. setDefaultTextEncodingName ("GBK"); myWebView. setWebViewClient (new WebViewClient (); myWebView. setWebChromeClient (new WebChromeClient () {@ Override public boolean onJsAlert (WebView view, String url, String message, JsResult result) {// TODO Auto-generated method stub return super. onJsAlert (view, url, message, result) ;}}); // Add an object so that javascript can access the object's method, myWebView. addJavascriptInterface (new WebAppInterface (this), "myInterfaceName"); // load the page: local html resource file, which is placed in the assets folder myWebView. loadUrl ("file: // android_asset/javascripttest.html"); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub // call the method myWebView in javascript in java. loadUrl ("javascript: myFunction ()") ;}}) ;}class WebAppInterface {Context mContext; WebAppInterface (Context c) {mContext = c ;}public void showToast (String toast) {Toast. makeText (mContext, toast, Toast. LENGTH_LONG ). show ();}}}

Javascript xml file

<Html> 

Layout file xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "match_parent" android: layout_height = "30dp" android: text = "javascript and android call each other" android: textSize = "20dp" android: gravity = "center" android: id = "@ + id/textView1"/> <WebView android: layout_below = "@ + id/textView1" android: id = "@ + id/javascriptWebview" android: layout_width = "match_parent" android: layout_height = "380dp"/> <Button android: id = "@ + id/uttonId" android: layout_below = "@ + id/javascriptWebview" android: layout_width = "match_parent" android: layout_height = "50dp" android: textSize = "20dp" android: gravity = "center" android: text = "android calls javascript"/> </RelativeLayout>

 

Thank you for your reference and have the opportunity to communicate with us! You are welcome to raise questions or learn new advanced technologies!

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.