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
 

DonManfred

Expert
Licensed User
1.
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png


2.
B4X:
Sub btn_click
dim btn as button = sender
Log($"The Button is "${btn.Tag}" - ${btn.Text}"$ )
End Sub
 
Upvote 0

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
 
Upvote 0

emexes

Expert
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.
 
Upvote 0
Top