Let’s say my textField is box1. I used someones recommendation on using box1.placeholder = “instructions” but I couldn’t get it to work and I couldn’t find it in the APIs. I’m using text wrangler if that means anything. Is there something that will work to my needs regardless if I’m coding for a droid or iphone? [import]uid: 35535 topic_id: 31326 reply_id: 331326[/import]
myTextField = native.newTextField(0,0,320,20)
myTextField.text = "Placeholder"
Then in the the event handler function where you show the keyboard, just set myTextField.text = “” before showing the keyboard.
[import]uid: 19626 topic_id: 31326 reply_id: 125288[/import]
Thanks for the feedback but I’m still not able to get it. Do you mind looking at my code? I’m relatively new with textfields and I know I’m making a silly mistake somewhere.
[lua]local function box1Handler (event)
if ( “began” == event.phase ) then
box1Title.text = “”
elseif ( “submitted” == event.phase ) then
– Hide keyboard
native.setKeyboardFocus( nil )
end
end
local box1Title = native.newTextField (x, y, display.contentWidth/5, display.contentHeight/8, box1Handler)
box1Title.x = display.contentWidth/2
box1Title.y = display.contentHeight/2
box1Title.size = 12
box1Title.text = “instructions”
box1Title.hasBackground = false
– box1Title:addEventListener (“userInput”, box1Handler) [/lua] [import]uid: 35535 topic_id: 31326 reply_id: 125301[/import]
Try this:
local box1Title
local function box1Handler (event)
if ( "began" == event.phase ) then
box1Title.text = ""
elseif ( "submitted" == event.phase ) then
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
box1Title = native.newTextField (x, y, display.contentWidth/5, display.contentHeight/8)
box1Title.x = display.contentWidth/2
box1Title.y = display.contentHeight/2
box1Title.size = 12
box1Title.text = "instructions"
box1Title.hasBackground = false
box1Title.userInput = box1Handler
box1Title:addEventListener ("userInput", box1Title)
The only change I made was to remove the eventHandler in the newTextField call because the documentation says: “It is recommended you do not use this argument and instead add a “userInput” event listener to the TextField object.”
You had the listener close. Corona SDK/Lua supports two types of listeners, function listeners like you had commented out and object listeners. The difference, if you look at the way I did it, I add a member to the object that has the same name as the event Type. In this case “userInput”. There I assign the function I want to use. Then in the :addEventListener() call, I pass the object, not the function. The result of this is that the parameter “self” gets passed automatically to your function. There is a lot of magic going on with this, but it should work.
Also notice that I defined box1Title at the top and then created it after the function. You were running into a “scope” issue in that you referenced box1Title in the function before it existed.
[import]uid: 19626 topic_id: 31326 reply_id: 125310[/import]
So if I still wasn’t able to get it to work, is that just an issue with the simulator that will fix up when I build on a device? [import]uid: 35535 topic_id: 31326 reply_id: 125315[/import]
Put a print statement at the top inside the handler function that does a:
print(event.phase)
and make sure you’re getting a “began” phase. [import]uid: 19626 topic_id: 31326 reply_id: 125318[/import]
myTextField = native.newTextField(0,0,320,20)
myTextField.text = "Placeholder"
Then in the the event handler function where you show the keyboard, just set myTextField.text = “” before showing the keyboard.
[import]uid: 19626 topic_id: 31326 reply_id: 125288[/import]
Thanks for the feedback but I’m still not able to get it. Do you mind looking at my code? I’m relatively new with textfields and I know I’m making a silly mistake somewhere.
[lua]local function box1Handler (event)
if ( “began” == event.phase ) then
box1Title.text = “”
elseif ( “submitted” == event.phase ) then
– Hide keyboard
native.setKeyboardFocus( nil )
end
end
local box1Title = native.newTextField (x, y, display.contentWidth/5, display.contentHeight/8, box1Handler)
box1Title.x = display.contentWidth/2
box1Title.y = display.contentHeight/2
box1Title.size = 12
box1Title.text = “instructions”
box1Title.hasBackground = false
– box1Title:addEventListener (“userInput”, box1Handler) [/lua] [import]uid: 35535 topic_id: 31326 reply_id: 125301[/import]
Try this:
local box1Title
local function box1Handler (event)
if ( "began" == event.phase ) then
box1Title.text = ""
elseif ( "submitted" == event.phase ) then
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
box1Title = native.newTextField (x, y, display.contentWidth/5, display.contentHeight/8)
box1Title.x = display.contentWidth/2
box1Title.y = display.contentHeight/2
box1Title.size = 12
box1Title.text = "instructions"
box1Title.hasBackground = false
box1Title.userInput = box1Handler
box1Title:addEventListener ("userInput", box1Title)
The only change I made was to remove the eventHandler in the newTextField call because the documentation says: “It is recommended you do not use this argument and instead add a “userInput” event listener to the TextField object.”
You had the listener close. Corona SDK/Lua supports two types of listeners, function listeners like you had commented out and object listeners. The difference, if you look at the way I did it, I add a member to the object that has the same name as the event Type. In this case “userInput”. There I assign the function I want to use. Then in the :addEventListener() call, I pass the object, not the function. The result of this is that the parameter “self” gets passed automatically to your function. There is a lot of magic going on with this, but it should work.
Also notice that I defined box1Title at the top and then created it after the function. You were running into a “scope” issue in that you referenced box1Title in the function before it existed.
[import]uid: 19626 topic_id: 31326 reply_id: 125310[/import]
So if I still wasn’t able to get it to work, is that just an issue with the simulator that will fix up when I build on a device? [import]uid: 35535 topic_id: 31326 reply_id: 125315[/import]
Put a print statement at the top inside the handler function that does a:
print(event.phase)
and make sure you’re getting a “began” phase. [import]uid: 19626 topic_id: 31326 reply_id: 125318[/import]
Thanks for the help robmiracle, the reason I wasn’t getting it to work was because I mispelled the function. So it works very well but there are two things I need help with.
-
Is there a way to have multiple listeners from different textFields all refer to this function when in the began phase? I messed around with self:removeSelf() before but I’m thinking this situation may be different.
-
Although visually it is working great in the simulator, I’m getting this message in the terminal and I don’t know if I should do anything to fix it.
[lua] 2012-11-05 16:58:23.922 Corona Simulator[5089:903] Exception detected while handling key input.
2012-11-05 16:58:23.923 Corona Simulator[5089:903] *** -[NSBigMutableString substringWithRange:]: Range or index out of bounds
logout[/lua] [import]uid: 35535 topic_id: 31326 reply_id: 129962[/import]
Thanks for the help robmiracle, the reason I wasn’t getting it to work was because I mispelled the function. So it works very well but there are two things I need help with.
-
Is there a way to have multiple listeners from different textFields all refer to this function when in the began phase? I messed around with self:removeSelf() before but I’m thinking this situation may be different.
-
Although visually it is working great in the simulator, I’m getting this message in the terminal and I don’t know if I should do anything to fix it.
[lua] 2012-11-05 16:58:23.922 Corona Simulator[5089:903] Exception detected while handling key input.
2012-11-05 16:58:23.923 Corona Simulator[5089:903] *** -[NSBigMutableString substringWithRange:]: Range or index out of bounds
logout[/lua] [import]uid: 35535 topic_id: 31326 reply_id: 129962[/import]