Android Question Does EditText have focus [Solved]

mangojack

Expert
Licensed User
Longtime User
One method ... setup a global flag and check that.

B4X:
Private flagEditTextFocus As Boolean
    
Sub EditText1_FocusChanged (HasFocus As Boolean)    
    If HasFocus Then
        flagEditTextFocus = True
    Else
        flagEditTextFocus = False
    End If
End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
B4X:
Sub et_EnterPressed
    Dim editer As EditText
    editer = Sender
    
    Dim jo As JavaObject
    jo.InitializeContext
    jo = edittext
    Dim hasfocus As Boolean = jo.RunMethod("hasFocus",Null)
    Log(editer.Tag & " hasfocus: " & hasfocus)
    
End Sub

Sub et_FocusChanged (HasFocus As Boolean)
    Dim editer As EditText
    editer = Sender
    Dim jo As JavaObject
    jo.InitializeContext
    jo = edittext
    Dim HasFocus As Boolean = jo.RunMethod("hasFocus",Null)
    Log(editer.Tag & " hasfocus: " & HasFocus)
    
End Sub

so, there appears to be a method called hasFocus() that's part of android's View class. i used a java object to get at it. it seemed to work ok.
if you only have 1 edittext view, then you don't need:
B4X:
   Dim editer As EditText
   editer = Sender
this part. (and, of course, you wouldn't need to reference the tag property. if you have multiple edittext views and you wanted to know which one had the focus, you would populate the tag property and then refer to it in the enter_press and/or focus_changed subs.

as i say, i tested it briefly, and it seemed happy. when i tapped the edittext to get its attention, it triggered true. when i tapped the enter key, it said true (of course, you wouldn't use the force_enter_key option unless the edittext was single line, which, in my case, it was)
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
B4X:
Sub et_EnterPressed
    Dim editer As EditText
    editer = Sender
   
    Dim jo As JavaObject
    jo.InitializeContext
    jo = edittext
    Dim hasfocus As Boolean = jo.RunMethod("hasFocus",Null)
    Log(editer.Tag & " hasfocus: " & hasfocus)
   
End Sub

Sub et_FocusChanged (HasFocus As Boolean)
    Dim editer As EditText
    editer = Sender
    Dim jo As JavaObject
    jo.InitializeContext
    jo = edittext
    Dim HasFocus As Boolean = jo.RunMethod("hasFocus",Null)
    Log(editer.Tag & " hasfocus: " & HasFocus)
   
End Sub

so, there appears to be a method called hasFocus() that's part of android's View class. i used a java object to get at it. it seemed to work ok.
if you only have 1 edittext view, then you don't need:
B4X:
   Dim editer As EditText
   editer = Sender
this part. (and, of course, you wouldn't need to reference the tag property. if you have multiple edittext views and you wanted to know which one had the focus, you would populate the tag property and then refer to it in the enter_press and/or focus_changed subs.

as i say, i tested it briefly, and it seemed happy. when i tapped the edittext to get its attention, it triggered true. when i tapped the enter key, it said true (of course, you wouldn't use the force_enter_key option unless the edittext was single line, which, in my case, it was)


Thanks drgottjr

Roger
 
Upvote 0
Top