mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-06-18 07:29:20 +08:00
add Timer sample app for android
This commit is contained in:
13
android-samples/Timer/.gitignore
vendored
Normal file
13
android-samples/Timer/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
.repl_history
|
||||
build
|
||||
tags
|
||||
.DS_Store
|
||||
nbproject
|
||||
.redcar
|
||||
#*#
|
||||
*~
|
||||
*.sw[po]
|
||||
.eprj
|
||||
.sass-cache
|
||||
.idea
|
||||
.dat*.*
|
||||
11
android-samples/Timer/Rakefile
Normal file
11
android-samples/Timer/Rakefile
Normal file
@@ -0,0 +1,11 @@
|
||||
# -*- 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 = 'Timer'
|
||||
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
|
||||
68
android-samples/Timer/app/main_activity.rb
Normal file
68
android-samples/Timer/app/main_activity.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
class TimerButtonListener < Java::Lang::Object
|
||||
def set_activity(activity)
|
||||
@activity = activity
|
||||
end
|
||||
|
||||
def onClick(view)
|
||||
@activity.toggleTimer
|
||||
end
|
||||
end
|
||||
|
||||
class TimerTask < Java::Util::TimerTask
|
||||
def set_activity(activity)
|
||||
@activity = activity
|
||||
end
|
||||
|
||||
def run
|
||||
@activity.handler.post -> { @activity.updateTimer }
|
||||
end
|
||||
end
|
||||
|
||||
class MainActivity < Android::App::Activity
|
||||
def onCreate(savedInstanceState)
|
||||
super
|
||||
@handler = Android::Os::Handler.new
|
||||
|
||||
layout = Android::Widget::LinearLayout.new(self)
|
||||
layout.orientation = Android::Widget::LinearLayout::VERTICAL
|
||||
|
||||
@label = Android::Widget::TextView.new(self)
|
||||
@label.text = 'Tap to start'
|
||||
@label.textSize = 40.0
|
||||
@label.gravity = Android::View::Gravity::CENTER_HORIZONTAL
|
||||
layout.addView(@label)
|
||||
|
||||
@button = Android::Widget::Button.new(self)
|
||||
@button.text = 'Start'
|
||||
listener = TimerButtonListener.new
|
||||
listener.set_activity self
|
||||
@button.onClickListener = listener
|
||||
layout.addView(@button)
|
||||
|
||||
self.contentView = layout
|
||||
end
|
||||
|
||||
def handler
|
||||
@handler
|
||||
end
|
||||
|
||||
def toggleTimer
|
||||
if @timer
|
||||
@timer.cancel
|
||||
@timer = nil
|
||||
@button.text = 'Start'
|
||||
else
|
||||
@timer = Java::Util::Timer.new
|
||||
@counter = 0
|
||||
task = TimerTask.new
|
||||
task.set_activity self
|
||||
@timer.schedule task , 0, 100
|
||||
@button.text = 'Stop'
|
||||
end
|
||||
end
|
||||
|
||||
def updateTimer
|
||||
@label.text = "%.1f" % (@counter += 0.1)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user