我准备了Ruby-Class:
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :Button, :LinearLayout, :TextView, :EditText
# http://xkcd.com/378/
class MinecraftDistanceCalculatorActivity
def onCreate(bundle)
super
set_title 'Minecraft Distance Calculator'
self.content_view =
linear_layout :orientation => :vertical do
@startx = text_view :text => 'Enter start x-axe', :id => 42,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@edit_text = edit_text :id => 44,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@startz = text_view :text => 'Enter start z-axe', :id => 45,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@edit_text = edit_text :id => 46,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@targetx = text_view :text => 'Enter target x-axe', :id => 47,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@edit_text = edit_text :id => 48,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@targetz = text_view :text => 'Enter target z-axe', :id => 49,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
@edit_text = edit_text :id => 50,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0
button :text => 'Calculate!',
:layout => {:width => :match_parent},
:id => 43, :on_click_listener => proc { distance }
# @text_view = text_view :text => "The distance is #{distance} blocks", :id => 50,
# :layout => {:width => :match_parent},
# :gravity => :center, :text_size => 16.0
end
rescue Exception
puts "Exception creating activity: #{$!}"
puts $!.backtrace.join("\n")
end
private
def self.distance(startx, startz, targetx, targetz)
calcx = startx - targetx
calcz = startz - targetz
calcdist = calcx**2 + calcz**2
distance = Math.sqrt(calcdist).round
toast 'Calculated distance'
return distance
end
现在我想使用特定参数startx,startz,targetx和targetz访问该按钮。 然后我计划将返回的变量“distance”作为text_field给出。 也许有人知道,怎么做?
答案 0 :(得分:1)
根据您的评论,您可以尝试:
:on_click_listener => proc { @distance = self.class.distance(@startx, @startz, @targetx, @targetz) }
然后,只需使用@distance
@text_view = text_view :text => "The distance is #{@distance} blocks", :id => 50,
:layout => {:width => :match_parent},
:gravity => :center, :text_size => 16.0