How Do You Exit Native Textfield?

I have a textfield. I can type in it, receive events, change text, but for some reason I cannot truly change the focus on the text field. I change the current stage focus to nil and the field focus to false, but for the rest of my app session I am stuck with this textfield’s cursor forever blinking. How do I get rid of this? 

To dismiss the keybaord:

    native.setKeyboardFocus( nil )

To dismiss the keybaord:

    native.setKeyboardFocus( nil )

Hey Rob, is there a way to get the keyboard focus? On iOS, the user can dismiss their keyboard. Is there a way to check for this? If I set the stage focus to the textfield/textbox, and the user dismisses the keyboard, the focus is still on the textbox. This leaves the user without a cursor for them to know they are still in the textbox. They then frantically try to scroll, and tap buttons thinking the app froze. 

Thanks

There should be an event.phase == “ended” that triggers when the keyboard is dismissed.

Hey Rob, is there a way to get the keyboard focus? On iOS, the user can dismiss their keyboard. Is there a way to check for this? If I set the stage focus to the textfield/textbox, and the user dismisses the keyboard, the focus is still on the textbox. This leaves the user without a cursor for them to know they are still in the textbox. They then frantically try to scroll, and tap buttons thinking the app froze. 

Thanks

There should be an event.phase == “ended” that triggers when the keyboard is dismissed.

Is there a way to get rid of the keyboard when the user taps anywhere else on the screen? I’ve tried using the documentation from online:

[lua]

local fieldHandler = function(event)

    return function(event)

        if (“began” == event.phase) then

            --nothing

        elseif (“ended” == event.phase) then

            native.setKeyboardFocus(nil)

        elseif (“submitted” == event.phase) then

            native.setKeyboardFocus(nil)

        end

    end

end


local commentsHandler = function(event)

    fieldHandler(function() return comments end)

end

.

.

.

.

comments.userInput = textListener

comments:addEventListener( “userInput”, commentsHandler )

[/lua]

However, when I tap elsewhere on the screen, the keyboard remains. Suggestions?

Hi @aaron34,

I think you might need to apply a touch listener to the stage and then pass “nil” to the native.setKeyboardFocus() API. Like this:

[lua]

local function clearKeyboard()

  native.setKeyboardFocus(nil)

end

display.currentStage:addEventListener( “tap”, clearKeyboard )

[/lua]

You could also probably write that as an inline function for the listener, if you prefer that way.

Brent

Thanks, Brent! That did the trick. 

Is there a way to get rid of the keyboard when the user taps anywhere else on the screen? I’ve tried using the documentation from online:

[lua]

local fieldHandler = function(event)

    return function(event)

        if (“began” == event.phase) then

            --nothing

        elseif (“ended” == event.phase) then

            native.setKeyboardFocus(nil)

        elseif (“submitted” == event.phase) then

            native.setKeyboardFocus(nil)

        end

    end

end


local commentsHandler = function(event)

    fieldHandler(function() return comments end)

end

.

.

.

.

comments.userInput = textListener

comments:addEventListener( “userInput”, commentsHandler )

[/lua]

However, when I tap elsewhere on the screen, the keyboard remains. Suggestions?

Hi @aaron34,

I think you might need to apply a touch listener to the stage and then pass “nil” to the native.setKeyboardFocus() API. Like this:

[lua]

local function clearKeyboard()

  native.setKeyboardFocus(nil)

end

display.currentStage:addEventListener( “tap”, clearKeyboard )

[/lua]

You could also probably write that as an inline function for the listener, if you prefer that way.

Brent

Thanks, Brent! That did the trick.