Android Question [Solved] There is something weird with ShowKeyboard

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have to 2 EditText, ArtCdTxt & QtyCdTxt.

There is an event for ArtCdTxt
B4X:
Sub ArtCdTxt_FocusChanged (HasFocus As Boolean)
    If(Not(HasFocus)) Then
        Msgbox("2","")
        ArtCdTxt.RequestFocus       
    End If
   
End Sub

Those code just for ex, when QtyTxt clicked, Message Box throw just one and then focus return to ArtCdTxt.

But if I add these event on QtyTxt
B4X:
Sub QtyTxt_FocusChanged (HasFocus As Boolean)
    If(HasFocus) Then
        IME.ShowKeyboard(QtyTxt)   
    End If
End Sub
Something weird happen. When I clicked QtyTxt, ArtCdTxt_FocusChanged is always trigger, Message box show up again and again in a loop.

Is there something wrong with my logic or there is a bug in ShowKeyboard?
 

JordiCP

Expert
Licensed User
Longtime User
Not expert, but I think it is your logic

When one of the EditText gains Focus, the other loses it. So, when one of the events is triggered saying it got focus, the other also does (saying it lost focus).

At start, let's suppose we have ArtCDTxt focused (A)

If you click QtyTxt when the focus is on ArtCdTxt, two events occur
ArtCDTxt --> Loses focus (a)
QtyTxt --> Gains focus (B)

On the first event, you try to get it again, two new events are fired:
ArtCDTxt --> Gains focus (A)
QtyTxt --> Loses focus. (b)

So here everything would have to stop and the sequence would have to be:

AaBbA

But it seems as is the call to IME.ShowKeyBoard returns the focus again when ready, so the sequence follows aBbAaBba......


The question is: why do you want to return always the focus to the first EditText?
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Sound logic, I changed QtyTxt event to this
B4X:
Sub QtyTxt_FocusChanged (HasFocus As Boolean)
    If(HasFocus) Then
        MsgBox("1","")
    End If
End Sub
Program runs ok, message box on QtyTxt event shows up ones, then focus return to AcCdTx.

I think ShowKeyboard triggered the event.
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Yes, care must be taken when performing actions that fire an event, which at the same time may fire a new event, since you may fall in a loop.

Strange, anyway
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
I would suggest that you log when each sub is entered and the state of HasFocus so you can see just where the logic leads you when you run the app.
B4X:
Sub ArtCdTxt_FocusChanged (HasFocus AsBoolean)
     Log("ArtCDTxt_FocusChanged: HasFocus = " & HasFocus)
     If(Not(HasFocus)) Then
          Msgbox("2","")
          ArtCdTxt.RequestFocus
     End If
End Sub

Sub QtyTxt_FocusChanged (HasFocus AsBoolean)
     Log("QtyTxt_FocusChanged: HasFocus = " & HasFocus)
     If(HasFocus) Then
          MsgBox("1","")
     End If
End Sub
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Found the problem!!

Definitely, ShowKeyBoard fire up an FocusChanged event on every EditText.

I changed the codes to this
B4X:
Sub ArtCdTxt_FocusChanged (HasFocus AsBoolean)
   If(Not(HasFocus)) Then
      if(DoingSameChecking = False) then
         IsArtErr = True 
         ArtCdTxt.RequestFocus 
      else
        IsArtErr = False
      End if  
   End If
End Sub

Sub QtyTxt_FocusChanged (HasFocus As Boolean)
    If(HasFocus) Then
        If(Not(IsArtErr)) Then 'Keyboard only shows up if no error found in ArtCdTxt
            QtyTxt.SelectAll           
            IME.ShowKeyboard(QtyTxt)   
        End If
    End If
End Sub
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
The purpose are :
1) after users input a value in ArtCdTxt, I need to find out if this input is a valid code
2) if it valid then go QtyTxt and display the virtual keyboad, waiting for input.
virtual keyboard must be called programaticly, cause sometime it doesn't show up if users does not click it
3) if it is not valid, Warning Message toast to users and focus set again to ArtCdTxt, ready for editing by user
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…