【发布时间】:2016-10-22 03:36:22
【问题描述】:
我想要完成的是,当我创建一个 native.newTextField 时,将 isSecure 选项设置为 false 并将占位符设置为“输入您的密码”
每当用户开始输入时,将文本字段更改为 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 值?始终保持
true,它不会干扰placeholder。 -
它至少会干扰我的做法。如果我先调用占位符,然后调用 isSecure,占位符的文本就会消失。之前:--此处占位符文本重置为“” passwordField.placeholder = “输入您的密码” passwordField.isSecure = true 之后:--此处占位符文本保留其值。 passwordField.isSecure = true passwordField.placeholder = "type your password" 感谢您帮助我解决这个问题。