Last active
December 18, 2015 12:09
-
-
Save simpleton/5780804 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//html code | |
<html> | |
<head> | |
<script type="text/javascript"> | |
function ClientSideMethod(message){ | |
var x=document.getElementById("te"); | |
x.innerHTML = message; | |
} | |
</script> | |
</head> | |
<body> | |
<div id="te"></div> | |
<button onClick="ClientSideMethod('client side method called')"> Call Javascript Method </button> | |
<button onClick="testClick('Hello aaa');">Call Java Activity Method</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//java code | |
public class MainActivity extends Activity { | |
private WebView webView; | |
private static final String TAG = "MainActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
this.webView = (WebView) findViewById(R.id.webview); | |
// requires javascript | |
this.webView.getSettings().setJavaScriptEnabled(true); | |
// set the html view to load | |
this.webView.loadUrl("file:///android_asset/test.html"); | |
// make this activity accessible to javascript | |
this.webView.addJavascriptInterface(this, "activity"); | |
this.webView.loadUrl("javascript:function testClick(aa) {var x=document.getElementById(\"te\");x.innerHTML = aa;activity.TestMethod(aa);}"); | |
} | |
@JavascriptInterface | |
public void TestMethod(String aa) | |
{ | |
Log.e(TAG, "TestMethod: " + aa); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment