Hello,
If I input spaces only then click OK button, the text is trimed and “OK” is not displayed on the simulator, but on Android yes. Is there a workaroud?
local function trim(str)
return string.gsub(str,"^%s*(.-)%s*$","%1")
end
local function touchOK(evt)
if evt.phase=="ended" then
fld.text=trim(fld.text)
if fld.text~="" then
display.newText("OK!",400,750,"Verdana",80)
end
end
return true
end
fld=native.newTextField(400,150,770,100)
rec=display.newRect(400,600,100,100)
rec:addEventListener("touch",touchOK)
-- Trim of spaces before/after/multiple together in middle.
local function trim(name)
local result
result = string.gsub(name, "%s+", " ")
result = string.gsub(result, "^%s*", "")
result = string.gsub(result, "%s*$", "")
return result
end
I am not sure exactly what’s going on, but you can try this code if it works.
I tried others, and yours just now, it’s the same behaviour.
I don’t understand why, that’s not logic. If the string is empty, why the text is displayed??
it seems the problem comes from the pressed button, because if I write this only:
fld.text=" "
fld.text=trim(fld.text)
if fld.text~="" then display.newText("OK!",400,750,"Verdana",80) end
That’s work, nothing is displayed
EDIT: I had an idea. I think there’s a delay between the input and it’s real value to update the string, so I did this and it works now
local function touchOK(evt)
if evt.phase=="ended" then
fld.text=trim(fld.text)
timer.performWithDelay(50,function()
if fld.text~="" then
display.newText("OK!",400,750,"Verdana",80)
end
end,1)
end
return true
end
local function touchOK(evt)
if evt.phase=="began" then
fld.text=trim(fld.text)
elseif evt.phase=="ended" then
if fld.text~="" then
display.newText("OK!",400,750,"Verdana",80)
end
end
return true
end```