Button.Pressed

Firpas

Active Member
Licensed User
Longtime User
Hi to evreybody:

In the designer, for button view there is a property named "pressed" as boolean.

How I can set it from code?
 

Firpas

Active Member
Licensed User
Longtime User
Thanks for your help
This method works but its effect quickly disappears.

I think I will use ToggleButtons

Best regards
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hello Klaus,
I want to decrement the number in lbl1 until the btnm is pressed, the following code doesn't work, after button_up event it goes back in button down even if i set that button is unpressed. could you please tell me how can handle this situation ?

Sub btnm_Down

SetPressed(Btnm,True)

Do While isPressed(Btnm)
If nr=1 Then
nr=49
lbl1.Text =nr
Else
nr=nr-1
lbl1.Text =nr
End If
Loop
End Sub

Sub btnm_Up
SetPressed(Btnm,False)
End Sub

Sub isPressed(btn As Button) As Boolean
Dim r As Reflector
r.Target = btn
Return r.RunMethod("isPressed")
End Sub

Sub SetPressed(Btn As Button, pressed As Boolean)
Dim r As Reflector
r.Target = Btn
r.RunMethod2("setPressed", pressed, "java.lang.boolean")
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You should use code tags when you want to show code (the button) !

I would suggest you to use a Timer to do this.
In Sub btnm_Down you set the Timer1.Enabled = True.
In Sub btnm_Up you set the Timer1.Enabled = False.
And put the decrementing code in the Timer1_Tick routine.
 

Attachments

  • upload_2014-2-21_13-8-25.png
    1,005 bytes · Views: 183
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User

Hi Klaus

I have 10 Buttons in a project. I have set button bNormal to pressed using
SetPressed(bNormal,True)
and the initial setting is correct (i.e button bNormal is Red)

I now want to toggle the state of the buttons with the code below but the toggle of the buttons does not work (Blue = not pressed, Red = pressed). I must be misinterpreting something. Sub isPressed keeps on coming back with "true" regardless of trying to change the state with
SetPressed(bNormal,True) or SetPressed(bNormal,False)

What am I missing? Below code is for buttons bNormal and bCircle only. If I change bCircle to bNormal in the If bb = "2" part of the code then button bNormal toggles between red and blue.

B4X:
Sub SetPressed(cmd As Button, Pressed As Boolean)
  Dim ref As Reflector
  ref.Target = cmd
  ref.RunMethod2("setPressed", Pressed, "java.lang.boolean")
End Sub

Sub ButtonPressed_Click

Dim b5 As Button
Dim bb As String

b5 = Sender
bb = b5.Tag
Log(bb)

If bb = "1" Then
  Log("here 1")
  Dim check As Boolean
  check = isPressed(bNormal)
  If check = True Then
    SetPressed(bNormal,False)
  Else
    SetPressed(bNormal,True)
  End If  
End If

If bb = "2" Then
  Log("here 2")
  Dim check As Boolean
  check = isPressed(bCircle)
  If check = True Then
    SetPressed(bCircle,False)
  Else
    SetPressed(bCircle,True)
  End If  
End If

End Sub

Sub isPressed(cmd As Button) As Boolean
    Dim r As Reflector
    Dim PressedState As Boolean
    r.Target = cmd
    PressedState =  r.RunMethod("isPressed")
    Return PressedState
  
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Do you have a small test project so we could test it.
Hoe did you set the colors of the buttons.
Are you trying to set a kind of radiobuttons with buttons ?
You could replace the buttons by togglebuttons, these ones have two states.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Do you have a small test project so we could test it.
Hoe did you set the colors of the buttons.
Are you trying to set a kind of radiobuttons with buttons ?
You could replace the buttons by togglebuttons, these ones have two states.

Hi Klaus

See attached. I have set the colors in the Designer.
See comments in code where I mention changing bCircle to bNormal. If Circle button is then pressed it toggles the Normal button
 

Attachments

  • TEST CMD BUTTONS.zip
    7.6 KB · Views: 145
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…