Android Question Seeking Help with Event Triggers in B4XMainPage and Page1

kira

Member
Licensed User
Hello everyone,

Although I roughly know that it has something to do with Wait For or Sleep, I have looked through many posts but still cannot find a proper solution. Could someone please guide me on which concepts I should focus on and how to modify the code?

Please see the attachment.

1.In B4XMainPage, why does Log(Send.tag) in edt_TextChanged() get triggered multiple times when the page first runs? My intention was to make the font color of the three EditText controls blue, but due to the multiple triggers, only the last EditText control's font color turns red.

2.In the Button2_Click() event of Page1, my intention is to call ReturnCtlValue() to change the values of the three EditText controls in B4XMainPage, which would then trigger the edt_TextChanged() event to update the text color of the three controls. However, the expected behavior only occurs when Sleep(0) is added in Button2_Click(). When Sleep(0) is commented out, Button2_Click() runs completely before triggering edt_TextChanged(), resulting in only edt3's text color changing, even though the values of the three EditText controls have been updated.

Any help would be greatly appreciated! Thank you!
 

Attachments

  • Project.zip
    15.9 KB · Views: 22

kira

Member
Licensed User
The code for the two modules is as follows

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
  
    Private pa1 As Page1
    Private ctlNameValueMap As Map
    Public ctlIndexMap As Map
    Public ScrView As ScrollView'ignore
    Private Send As B4XView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

    ctlNameValueMap.Initialize
    ctlIndexMap.Initialize


    Dim a() As String=Array As String("edt1","edt2","edt3")
    Dim i As Int

    ScrView.Initialize(1000dip) '
    Root.AddView(ScrView,0,0,100%x,100%y)
    Sleep(0)
    For i=0 To a.Length-1
        Dim edtCtl As EditText'
        edtCtl.Initialize("edt")
        edtCtl.Tag=a(i)
        edtCtl.TextColor=xui.Color_Blue
        edtCtl.InputType=edtCtl.INPUT_TYPE_PHONE
          
        ScrView.Panel.AddView(edtCtl, 30dip,100dip* i, 300dip, 100dip)
        ctlNameValueMap.Put(a(i),(i+1)*1000)
        ctlIndexMap.Put(a(i),ctlIndexMap.Size) 
        edtCtl.Text=(i+1)*1000
    Next

End Sub


Private Sub Button1_Click
'    xui.MsgboxAsync("Hello world!", "B4X")
    pa1.Initialize
    B4XPages.AddPage("pa1",pa1)
    pa1.Show(Me)
  
End Sub


Private Sub edt_TextChanged (Old As String, New As String)
    Send=Sender
    Log (Send.tag)
    Dim t As String=ctlNameValueMap.Get(Send.tag)
    If t=New Then
        Send.TextColor=xui.Color_Blue
    Else
        Send.TextColor=xui.Color_Red
    End If
  
End Sub


Page1:

Page1:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
  
    Private mCallback As B4XMainPage
    Private CallbackCtlIndexMap As Map
  
  
  
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("ex1")
  
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Public Sub Show(Callback As Object)

    mCallback = Callback
    B4XPages.ShowPage(B4XPages.GetPageId(Me))
    CallbackCtlIndexMap=mCallback.ctlIndexMap
End Sub


Private Sub Button2_Click
  
    'Change the values of edt1 and edt2 on the B4XMainPage in this page.
    ReturnCtlValue("edt1","1001")
'    Sleep(0)
    ReturnCtlValue("edt2","2001")
'    Sleep(0)
    ReturnCtlValue("edt3","3001")
End Sub


Private Sub ReturnCtlValue(CtlName As String,New As String)
    Dim indexs As Int
    Dim mCallbackView As B4XView
  
    indexs=CallbackCtlIndexMap.Get(CtlName)
    Dim V As ScrollView=  mCallback.ScrView
    mCallbackView=V.Panel.GetView(indexs)
    mCallbackView.text = New
  
End Sub
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Try this ,see if it works for you
 

Attachments

  • Project.zip
    15.9 KB · Views: 24
Upvote 0

kira

Member
Licensed User
Try this ,see if it works for you
Thank you, teddybear, for modifying the code in the B4XMainPage module's B4XPage_Created() event, which helped resolve the first issue, i.e., the last edt3 control now correctly displays the font in blue. However, I still have a question. When I step through the code using F8, the B4XPage_Created() event in the B4XMainPage module triggers the edt_TextChanged() event, and Log(Send.tag) returns three times, i.e., edt1, edt2, edt3. But when the code runs continuously, Log(Send.tag) returns six times, i.e., edt1, edt1, edt2, edt2, edt3, edt3. Although this does not affect the functionality, I feel like I am missing a key concept. I believe this concept would be helpful in resolving the second issue. Could you please provide further guidance? Thank you.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I don't think the edt_TextChanged was fired 3 times using F8. it should be 6 times
Why was it triggered 6 times? it is that you changed the text of edtctl 6 times.
 
Upvote 0

kira

Member
Licensed User
I don't think the edt_TextChanged was fired 3 times using F8. it should be 6 times
Why was it triggered 6 times? it is that you changed the text of edtctl 6 times.
I'm sorry, teddybear, there was a mistake on my end during debugging. You were right. I set the breakpoint at edtCtl.Text = (i + 1) * 1000 in B4XPage_Created(), and debugged step-by-step using F8, which required manually pressing F8 multiple times. The Log(Send.tag) returned edt1, edt2, edt3, edt3, edt3, edt3. It happened 6 times, but I don't understand why, after the first three executions, it triggered three more times in edt_TextChanged()?By the way, could you guide me on how to handle the second question?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
You changed the text twice in each loop of adding the edtCtl.
B4X:
edtCtl.InputType=edtCtl.INPUT_TYPE_PHONE
edtCtl.Text=(i+1)*1000
 
Upvote 0

kira

Member
Licensed User
You changed the text twice in each loop of adding the edtCtl.
B4X:
edtCtl.InputType=edtCtl.INPUT_TYPE_PHONE
edtCtl.Text=(i+1)*1000
Once again, thanks to teddybear, I also did a verification and found out that .InputType can also trigger the _TextChanged event. Thank you again.

My second question, I sincerely hope everyone can help me out.

2.In the Button2_Click() event of Page1, my intention is to call ReturnCtlValue() to change the values of the three EditText controls in B4XMainPage, which would then trigger the edt_TextChanged() event to update the text color of the three controls. However, the expected behavior only occurs when Sleep(0) is added in Button2_Click(). When Sleep(0) is commented out, Button2_Click() runs completely before triggering edt_TextChanged(), resulting in only edt3's text color changing, even though the values of the three EditText controls have been updated.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
For your 2nd question,did you test the project I uploaded at post #3?
 
Upvote 0

kira

Member
Licensed User
For your 2nd question,did you test the project I uploaded at post #3?
Yes, teddybear, I have tested the project on the 3rd floor (#3). The issue still exists. When clicking Button2_Click() on Page1, it calls ReturnCtlValue() to change the values of the three EditText controls on B4XMainPage. The values of edt1, edt2, and edt3 are successfully changed to 1001, 2001, and 3001 respectively, which meets my design requirements. It also triggers the edt_TextChanged() event. However, only the text color of edt3 turns red. I’ve tested it and confirmed that the edt_TextChanged() event is triggered three times. But since Send is the last edt3, only the last edt3 control changes its text color.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Yes, teddybear, I have tested the project on the 3rd floor (#3). The issue still exists. When clicking Button2_Click() on Page1, it calls ReturnCtlValue() to change the values of the three EditText controls on B4XMainPage. The values of edt1, edt2, and edt3 are successfully changed to 1001, 2001, and 3001 respectively, which meets my design requirements. It also triggers the edt_TextChanged() event. However, only the text color of edt3 turns red. I’ve tested it and confirmed that the edt_TextChanged() event is triggered three times. But since Send is the last edt3, only the last edt3 control changes its text color.
This is because you were running it in debug mode, in release mode it works.
 
Upvote 0

kira

Member
Licensed User
For the second question, my understanding is as follows: I think this issue is related to "Wait for," which in turn is connected to the message loop mechanism (I haven't mastered this technology yet, so if anyone sees this, they can guide me on the direction). This is because when I add Sleep(0), it works. In other words, the Button2_Click() event has triggered the edt_TextChanged() event, but since the Send in the edt_TextChanged() event triggers the control edt3 via ReturnCtlValue("edt3", "3001"), it continuously triggers the edt_TextChanged() event to change the text color of edt3. Therefore, this is related to a "Wait for" or similar mechanism. My alternative solution is to add logic to directly change the text color inside ReturnCtlValue(), but I feel like I'm missing a key concept here. Could someone please guide me?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Could someone please guide me?
start here

 
Upvote 0
Top