MainActivity is as follows:
[Java]
Package cn. testjavascript;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. webkit. WebView;
Import android. widget. Button;
Import android. app. Activity;
/**
* Demo description:
* JavaScript and Android Methods
* Mutual calls
*
* References:
**/
Public class MainActivity extends Activity {
Private WebView mWebView;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
// The following Javascript calls Java --------------------------
MWebView = (WebView) findViewById (R. id. webView );
InitWebViewSettings ();
// Load the local Html file
MWebView. loadUrl ("file: // android_asset/test.html ");
// Note:
// The second parameter in addJavascriptInterface
// It represents the alias of our java object javaClass, so that js can use this alias to call the method in Android
// In the js Code:
// Window. testjs. firstFunction
// Window. testjs. secondFunction
JavaClass javaClass = new JavaClass (MainActivity. this );
MWebView. addJavascriptInterface (javaClass, "testjs ");
// Java calls Javascript --------------------------
// Note:
// When you call a Javascript method with Parameters
// Write the parameters.
// MWebView. loadUrl ("javascript: javacalljswithargs (" + "'stringarg '" + ")");
// It is incorrect to leave no single quotes
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Call the method without parameters in Javascript
MWebView. loadUrl ("javascript: javacalljs ()");
// Call the method with parameters in Javascript
MWebView. loadUrl ("javascript: javacalljswithargs (" + "'stringarg '" + ")");
}
});
}
Private void initWebViewSettings (){
MWebView. setVerticalScrollBarEnabled (false );
MWebView. setHorizontalScrollBarEnabled (false );
MWebView. getSettings (). setJavaScriptEnabled (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setDomStorageEnabled (true );
MWebView. getSettings (). setPluginsEnabled (true );
MWebView. requestFocus ();
MWebView. getSettings (). setUseWideViewPort (true );
MWebView. getSettings (). setLoadWithOverviewMode (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setBuiltInZoomControls (true );
}
}
Package cn. testjavascript;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. webkit. WebView;
Import android. widget. Button;
Import android. app. Activity;
/**
* Demo description:
* JavaScript and Android Methods
* Mutual calls
*
* References:
*
*/
Public class MainActivity extends Activity {
Private WebView mWebView;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
// The following Javascript calls Java --------------------------
MWebView = (WebView) findViewById (R. id. webView );
InitWebViewSettings ();
// Load the local Html file
MWebView. loadUrl ("file: // android_asset/test.html ");
// Note:
// The second parameter in addJavascriptInterface
// It represents the alias of our java object javaClass, so that js can use this alias to call the method in Android
// In the js Code:
// Window. testjs. firstFunction
// Window. testjs. secondFunction
JavaClass javaClass = new JavaClass (MainActivity. this );
MWebView. addJavascriptInterface (javaClass, "testjs ");
// Java calls Javascript --------------------------
// Note:
// When you call a Javascript method with Parameters
// Write the parameters.
// MWebView. loadUrl ("javascript: javacalljswithargs (" + "'stringarg '" + ")");
// It is incorrect to leave no single quotes
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Call the method without parameters in Javascript
MWebView. loadUrl ("javascript: javacalljs ()");
// Call the method with parameters in Javascript
MWebView. loadUrl ("javascript: javacalljswithargs (" + "'stringarg '" + ")");
}
});
}
Private void initWebViewSettings (){
MWebView. setVerticalScrollBarEnabled (false );
MWebView. setHorizontalScrollBarEnabled (false );
MWebView. getSettings (). setJavaScriptEnabled (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setDomStorageEnabled (true );
MWebView. getSettings (). setPluginsEnabled (true );
MWebView. requestFocus ();
MWebView. getSettings (). setUseWideViewPort (true );
MWebView. getSettings (). setLoadWithOverviewMode (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setBuiltInZoomControls (true );
}
}
JavaClass is as follows:
[Java]
Package cn. testjavascript;
Import android. content. Context;
Import android. widget. Toast;
Public class JavaClass {
Private Context mContext;
Public JavaClass (Context context ){
This. mContext = context;
}
Public void firstFunction (){
Toast. makeText (mContext, "Javascipt calls methods in Java, without Parameters", Toast. LENGTH_SHORT). show ();
}
Public void secondFunction (String string ){
Toast. makeText (mContext, "Javascipt calls the method in Java and carries the following parameters:" + string, Toast. LENGTH_SHORT). show ();
}
}
Package cn. testjavascript;
Import android. content. Context;
Import android. widget. Toast;
Public class JavaClass {
Private Context mContext;
Public JavaClass (Context context ){
This. mContext = context;
}
Public void firstFunction (){
Toast. makeText (mContext, "Javascipt calls methods in Java, without Parameters", Toast. LENGTH_SHORT). show ();
}
Public void secondFunction (String string ){
Toast. makeText (mContext, "Javascipt calls the method in Java and carries the following parameters:" + string, Toast. LENGTH_SHORT). show ();
}
}
Main. xml is as follows:
[Html]
<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"
>
<WebView
Android: id = "@ + id/webView"
Android: layout_width = "fill_parent"
Android: layout_height = "370dip"
Android: layout_centerHorizontal = "true"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Java calls Javascript after clicking"
Android: layout_alignParentBottom = "true"
/>
</RelativeLayout>
<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"
>
<WebView
Android: id = "@ + id/webView"
Android: layout_width = "fill_parent"
Android: layout_height = "370dip"
Android: layout_centerHorizontal = "true"
/>
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Java calls Javascript after clicking"
Android: layout_alignParentBottom = "true"
/>
</RelativeLayout>
Test.html:
[Html]
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Script type = "text/javascript">
Function javacalljs (){
Document. getElementById ("content"). innerHTML + =
"<Br \> java calls javascript Functions without Parameters ";
}
Function javacalljswithargs (arg ){
Document. getElementById ("content"). innerHTML + =
("<Br \> java calls the javascript function. Parameter:" + arg );
}
</Script>
</Head>
<Body>
Test interaction between Android and Javascript <br/>
<Br>
<A onClick = "window. testjs. firstFunction ()"> Javascript calls java code after clicking </a> <br/>
<Br>
<A onClick = "window. testjs. secondFunction ('arg ')"> after clicking the button, Javascript calls java code and carries parameters. </a>
<Br/>
<Br>
<Div id = "content"> The following information is prompted when java calls Javascript: </div>
</Body>
</Html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Script type = "text/javascript">
Function javacalljs (){
Document. getElementById ("content"). innerHTML + =
"<Br \> java calls javascript Functions without Parameters ";
}
Function javacalljswithargs (arg ){
Document. getElementById ("content"). innerHTML + =
("<Br \> java calls the javascript function. Parameter:" + arg );
}
</Script>
</Head>
<Body>
Test interaction between Android and Javascript <br/>
<Br>
<A onClick = "window. testjs. firstFunction ()"> Javascript calls java code after clicking </a> <br/>
<Br>
<A onClick = "window. testjs. secondFunction ('arg ')"> after clicking the button, Javascript calls java code and carries parameters. </a>
<Br/>
<Br>
<Div id = "content"> The following information is prompted when java calls Javascript: </div>
</Body>
</Html>