Android Question What is the Button i Clicked?

jeronimovilar

Member
Licensed User
How can i get the button_click specify?

dim btnImage As Button
For i=1 To 5
btnImage.Initialize("btn")
btnImage.Tag = i
btnImage.Text = "Btn #" & i

Scrol.Panel.AddView(btnImage, 10%x, Scrol.Panel.Height, 80%x, 50dip)
Scrol.Panel.Height = Scrol.Panel.Height + 60dip
Scrol.Panel.Invalidate
Next

Sub btn_click
Log("The Button is ???" ) ' btnImage.tag??? btnImage.name??
End Sub
 

mc73

Well-Known Member
Licensed User
B4X:
sub btn_click
     private btn as button=sender
     private tag as string=btn.tag
     log("clicked "&tag)
end sub
 

emexes

Well-Known Member
Licensed User
If the buttons have dramatically different functions (like, perhaps you are evenly spacing out five control buttons of an audio/video player), you could instead modify the event handler prefix, eg:

B4X:
Dim ButtonName(5) As String = Array As String("Stop", "Rewind", "Play", "FastForward", "Pause")
For I = 1 To 5
    btnImage.Initialize("btn" & ButtonName(I - 1))
    '...
Next

Sub btnStop_Click
    'do stop stuff
End Sub

Sub btnRewind_Click
    'do rewind stuff
End Sub

Sub btnPlay_Click
    'do play stuff
End Sub

'etc
I expect that the tag way is better in most use-cases... but not always. Never hurts to have a spare arrow in the quiver.
 
Top