Android Question Knowing which view (EditText) has focus

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I have several EditText and I would like to know which of them has the focus.
How can I do it?
Thank you very much.
 

mangojack

Expert
Licensed User
Longtime User
If your EditTexts shared the same Focus Event you could use the Sender object and read the Tag property.

if not , this old thread (that I found by Searching the Forum) could offer a solution.
Determine which EditText has Focus
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hi, thanks for your reply, although I don't think it works for me, what I need is something similar to this:
B4X:
button1_click
  if edit1 has the focus then
    do one
  if edit2 has the focus then
    do two
  if edit3 has the focus then
    do three
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi:

Have you tried the posted solution?
I've tested it, and it works:

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
    Private EditFocus As EditText
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")
    EditText1.Tag = "EditText1"
    EditText2.Tag = "EditText2"
    EditText3.Tag = "EditText3"
End Sub

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

Private Sub Button1_Click
    xui.MsgboxAsync("The focus is on " & EditFocus.Tag, "Focus")
End Sub

Private Sub EditText_FocusChanged (HasFocus As Boolean)
    If HasFocus Then
        EditFocus = Sender
    Else
        EditFocus = Null
    End If
End Sub
 
Upvote 1

klaus

Expert
Licensed User
Longtime User
Or

B4X:
Sub Button1_Click
    If EditText1.As(JavaObject).RunMethod("hasFocus", Null) = True Then
        Log("EditText1 has focus")
    Else If EditText2.As(JavaObject).RunMethod("hasFocus", Null) = True Then
        Log("EditText2 has focus")
    Else If EditText3.As(JavaObject).RunMethod("hasFocus", Null) = True Then
        Log("EditText3 has focus")
    End If
End Sub
 
Upvote 1

vecino

Well-Known Member
Licensed User
Longtime User
Thanks folks, all the proposed options work for me for what I need.
I didn't test correctly yesterday, sorry.
Thank you very much!!!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…