Corona SDK - native.newTextField - 失去焦点

Corona sdk - native.newTextField - lose focus

我想要完成的是在创建 native.newTextField 时将 isSecure 选项设置为 false 并将占位符设置为 "type your password"

并且每当用户开始输入时,将文本字段更改为 isSecure = true,以具有 * 符号。 使用我的方法时,我的文本字段在第一个字母后失去焦点。

这是我的代码:

local function fieldHandler( event )
    if ( "began" == event.phase ) then
        -- This is the "keyboard has appeared" event
        -- In some cases you may want to adjust the interface when the keyboard appears.
    elseif ( "ended" == event.phase ) then
        -- This event is called when the user stops editing a field: for example, when they touch a different field
        if(event.target.id == "password" and event.target.text == "")then
            event.target.isSecure = false
            passwordField.placeholder = "type your password"
        end

    elseif ( "editing" == event.phase ) then
        if(event.target.id == "password" and string.len(event.target.text) >0 ) then
            event.target.isSecure = true
            --native.setKeyboardFocus( event.target )   
        end
    elseif ( "submitted" == event.phase ) then

        -- Hide keyboard
        native.setKeyboardFocus( nil )
    end

结束

--场景:展示 --做了

        local passwordField = native.newTextField( _CX - fieldWidth/2, passwordFieldBg.y, fieldWidth, 40 )
    passwordField.id = "password"
    passwordField:addEventListener( "userInput", fieldHandler ) 
    group:insert( passwordField)
    passwordField.hasBackground = false
    passwordField.anchorX = 0
    passwordField.placeholder = "type your password"
    passwordField.autocorrectionType = "UITextAutocorrectionTypeNo"
    passwordField.isSecure = false
    passwordField.font =  native.newFont( gM.fontDin )
    passwordField:resizeFontToFitHeight()

请帮我解决这个问题,或者我可以使用的其他方法。

谢谢,

版本 2016.2883 (2016.5.17)

我认为没有必要更改 isSecure 值,但您可以将其移至 began 阶段:

if ( "began" == event.phase ) then
    -- This is the "keyboard has appeared" event
    -- In some cases you may want to adjust the interface when the keyboard appears.
    if (event.target.id == "password") then
        event.target.isSecure = true