mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-03-29 22:43:28 +08:00
add WebViewDemo sample
This commit is contained in:
12
android-samples/WebViewDemo/Rakefile
Normal file
12
android-samples/WebViewDemo/Rakefile
Normal file
@@ -0,0 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
$:.unshift("../../lib")
|
||||
require 'motion/project/template/android'
|
||||
|
||||
Motion::Project::App.setup do |app|
|
||||
# Use `rake config' to see complete project settings.
|
||||
app.name = 'WebViewDemo'
|
||||
app.main_activity = 'WebViewDemo'
|
||||
app.api_version = '18'
|
||||
app.sdk_path = File.expand_path('~/src/android-sdk-macosx')
|
||||
app.ndk_path = File.expand_path('~/src/android-ndk-r9d')
|
||||
end
|
||||
51
android-samples/WebViewDemo/app/WebViewDemo.rb
Normal file
51
android-samples/WebViewDemo/app/WebViewDemo.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class WebViewDemo < Android::App::Activity
|
||||
def onCreate(savedInstanceState)
|
||||
super
|
||||
@handler = Android::Os::Handler.new
|
||||
|
||||
@webview = Android::Webkit::WebView.new(self)
|
||||
settings = @webview.getSettings
|
||||
settings.savePassword = false
|
||||
settings.saveFormData = false
|
||||
settings.javaScriptEnabled = true
|
||||
settings.supportZoom = false
|
||||
|
||||
@webview.webChromeClient = MyWebChromeClient.new
|
||||
js_interface = DemoJavaScriptInterface.new
|
||||
js_interface.set_context(self)
|
||||
@webview.addJavascriptInterface(js_interface, "demo")
|
||||
|
||||
@webview.loadUrl("file:///android_asset/demo.html")
|
||||
|
||||
self.contentView = @webview
|
||||
end
|
||||
|
||||
def webview
|
||||
@webview
|
||||
end
|
||||
|
||||
def handler
|
||||
@handler
|
||||
end
|
||||
end
|
||||
|
||||
class DemoJavaScriptInterface < Java::Lang::Object
|
||||
def set_context(context)
|
||||
@context = context
|
||||
end
|
||||
|
||||
__annotation__('@android.webkit.JavascriptInterface')
|
||||
def clickOnAndroid
|
||||
# This method will be called from another thread, and WebView calls must
|
||||
# happen in the main thread, so we dispatch it via a Handler object.
|
||||
@context.handler.post -> { @context.webview.loadUrl("javascript:wave()") }
|
||||
end
|
||||
end
|
||||
|
||||
class MyWebChromeClient < Android::Webkit::WebChromeClient
|
||||
def onJsAlert(view, url, message, result)
|
||||
puts message
|
||||
result.confirm
|
||||
true
|
||||
end
|
||||
end
|
||||
BIN
android-samples/WebViewDemo/resources/android_normal.png
Executable file
BIN
android-samples/WebViewDemo/resources/android_normal.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 3.6 KiB |
BIN
android-samples/WebViewDemo/resources/android_waving.png
Executable file
BIN
android-samples/WebViewDemo/resources/android_waving.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 3.6 KiB |
20
android-samples/WebViewDemo/resources/demo.html
Executable file
20
android-samples/WebViewDemo/resources/demo.html
Executable file
@@ -0,0 +1,20 @@
|
||||
<html>
|
||||
<script language="javascript">
|
||||
/* This function is invoked by the activity */
|
||||
function wave() {
|
||||
document.getElementById("droid").src="android_waving.png";
|
||||
alert("Hello from JavaScript!");
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
<!-- Calls into the javascript interface for the activity -->
|
||||
<a onClick="window.demo.clickOnAndroid()"><div style="width:80px;
|
||||
margin:0px auto;
|
||||
padding:10px;
|
||||
text-align:center;
|
||||
border:2px solid #202020;" >
|
||||
<img id="droid" src="android_normal.png"/><br>
|
||||
Click me!
|
||||
</div></a>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user