Android Question Checkboxes

Arturs

Member
Licensed User
Longtime User
Hi

I created eight checkboxes.

B4X:
Dim b as Byte

Sub CheckBox1_CheckedChange(Checked As Boolean)
    
If CheckBox1.Checked = True Then Bit.And(b, 1)
else Bit.Or(b, 1)

endif

End Sub

Sub CheckBox2_CheckedChange(Checked As Boolean)

If CheckBox2.Checked = True Then Bit.And(b,2)
else Bit.Or(b, 2)
endif

End Sub
Sub CheckBox3_CheckedChange(Checked As Boolean)
...
End Sub
Sub CheckBox4_CheckedChange(Checked As Boolean)
...
End Sub
Sub CheckBox5_CheckedChange(Checked As Boolean)
...
End Sub
Sub CheckBox6_CheckedChange(Checked As Boolean)
...
End Sub
Sub CheckBox7_CheckedChange(Checked As Boolean)
...
End Sub
...
Sub CheckBox8_CheckedChange(Checked As Boolean)

I would not like to write very similar code in each checkboxes

I would like to use something like this:

B4X:
Sub Check_Checkboxes (number As Int)
   
If CheckBox[number].Checked = True Then Bit.And(b, number)
else Bit.Or(b, number)

End Sub

and use the following code for each checkboxes

B4X:
Sub CheckBox1_CheckedChange(Checked As Boolean)
    
Check_Checkboxes(1)

endif



Regards
Artur
 

DonManfred

Expert
Licensed User
Longtime User
Get some inspiration by this code

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private chklist As List
    Private chkarr(8) As CheckBox

    Private CheckBox1 As CheckBox
    Private CheckBox2 As CheckBox
    Private CheckBox3 As CheckBox
    Private CheckBox4 As CheckBox
    Private CheckBox5 As CheckBox
    Private CheckBox6 As CheckBox
    Private CheckBox7 As CheckBox
    Private CheckBox8 As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("EightCheckboxes")
    ' Fill Checkboxarray (Solution 1)
    chkarr(0) = CheckBox1
    chkarr(1) = CheckBox2
    chkarr(2) = CheckBox3
    chkarr(3) = CheckBox4
    chkarr(4) = CheckBox5
    chkarr(5) = CheckBox6
    chkarr(6) = CheckBox7
    chkarr(7) = CheckBox8
    '
    ' Trying Solution 1
    '
    ' Set all Textcolor to Blue
    For i = 0 To chkarr.Length-1
        chkarr(i).TextColor = Colors.Blue
    Next
  
  
  
  
    ' Fill Checkboxlist (Solution 2)
    chklist.Initialize
    chklist.Add(CheckBox1)
    chklist.Add(CheckBox2)
    chklist.Add(CheckBox3)
    chklist.Add(CheckBox4)
    chklist.Add(CheckBox5)
    chklist.Add(CheckBox6)
    chklist.Add(CheckBox7)
    chklist.Add(CheckBox8)
    '
    ' Trying Solution 2
    '
    ' The List contains OBJECTs. To use it as a Checkbox
    ' you first need to cast the Object to an checkbox
    '
    ' Here we´ll set all Checkboxes to checked
    For i = 0 To chklist.Size-1
        Dim chk As CheckBox = chklist.Get(i)
        chk.Checked = True
    Next
  
End Sub

PS: Welcome in our comunity!
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
Set all your checkboxes to the same event name. set each checkbox tag to its index. Then you can use Sender to find out which checkbox was checked.
B4X:
Sub CheckBox_CheckedChange(Checked As Boolean)
    Dim CHeck As CheckBox = Sender
    Log (CHeck.Tag & " Was changed to " & Checked)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…