Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created August 9, 2013 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkAmi/6197685 to your computer and use it in GitHub Desktop.
Save thinkAmi/6197685 to your computer and use it in GitHub Desktop.
Rubotoで動的にTextViewを追加・削除するサンプル
# encoding: utf-8
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :Button, :LinearLayout, :TextView
java_import 'android.util.Log'
class DynamicGuiActivity
def onCreate(bundle)
super
Log.v "LOG", "onCreate"
addition_click = proc do
change(toast: true)
end
@view = linear_layout orientation: :vertical do
@text_view = text_view text: 'ボタンを押してね', text_size: 20
@button_view = button text: '追加', on_click_listener: ClickListener.new(self)
@toast_view = button text: 'Add with toast', on_click_listener: addition_click
end
self.content_view = @view
end
def change(args)
if @display_view.nil?
@display_view = text_view text: 'Hello World !', text_size: 48
@view.addView(@display_view)
@button_view.text = '削除'
@toast_view.text = 'Remove with toast'
toast '追加しました' if args[:toast]
else
@view.removeView(@display_view)
@display_view = nil
@button_view.text = '追加'
@toast_view.text = 'Add with toast'
toast '削除しました' if args[:toast]
end
end
end
class ClickListener
def initialize(activity)
@activity = activity
end
def onClick(view)
Log.v "LOG", "onClick"
@activity.change(toast: false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment