(Solved)Native Keyboard 2

I am trying to get the native keyboard 2 display to work on one of my screens. I stripped away everything I didn’t want or thought I needed in the tutorial and build it and it looked fine on my device. I then want to move that coding to my app to make it work there. So basically I had my screen already set up I just need to move the code to my screen and make it work. I am now getting an error when I try to load that screen. I was not sure where I needed to post the coding to make it work properly. Could somebody tell me what I’m doing wrong?

The lines I add from the tutorial are Lines 10 - 12 and Lines 137 - 233

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local ui = require(“ui”)

local tHeight – forward reference
local physics = require (“physics”)
physics.start ()
local background = display.newImage (“blackbackground.png”)
background.x = 240
localGroup:insert(background)
–> This sets the background

local screen6 = display.newImage (“screen6.png”)
screen6.x = 240
localGroup:insert(screen6)
–> This sets Screen6

local house = display.newImage (“house.png”)
house.x = 45
house.y = 35
house.xScale = .5
house.yScale = .5
localGroup:insert(house)
–>This places the House

local function touchedHouse (event)
if (“ended” == event.phase) then
director:changeScene (“titlescreen”)
media.playEventSound( “click_x.caf” )
end
end

house:addEventListener (“touch”, touchedHouse)

local numberTwo = display.newImage (“numberTwo.png”)
numberTwo.x = 240
numberTwo.y = 145
numberTwo.xScale = .5
numberTwo.yScale = .5
localGroup:insert(numberTwo)
–>This places the number two

local numberThree = display.newImage (“numberThree.png”)
numberThree.x = 240
numberThree.y = 200
numberThree.xScale = .5
numberThree.yScale = .5
localGroup:insert(numberThree)
–>This places the number three

local numberFour = display.newImage (“numberFour.png”)
numberFour.x = 240
numberFour.y = 270
numberFour.xScale = .5
numberFour.yScale = .5
localGroup:insert(numberFour)
–>This places the number four

– Handlers for login dialog UI

local function onnumberTwo( event )
if ( “began” == event.phase ) then
– Note: this is the “keyboard appearing” event
– In some cases you may want to adjust the interface while the keyboard is open.

elseif ( “submitted” == event.phase ) then
– Automatically tab to password field if user clicks “Return” on iPhone keyboard (convenient!)
native.setKeyboardFocus( passwordField )
end
end

local function onnumberThree( event )
– Hide keyboard when the user clicks “Return” in this field
if ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end

numberTwo = native.newTextField( 50, 150, 220, 36, onNumberTwo )
numberTwo.font = native.newFont( native.systemFontBold, 24 )
numberTwo.text = “”
numberTwo:setTextColor( 51, 51, 122, 255 )

numberThree = native.newTextField( 50, 210, 220, 36, onNumberTwo )
numberThree.font = native.newFont( native.systemFontBold, 24 )
numberThree.text = “”
numberThree.isSecure = true
numberThree:setTextColor( 51, 51, 122, 255 )
local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 428
smallbrain.y = 280
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
director:changeScene (“screen7a”)
media.playEventSound( “click_x.caf” )
end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)

local backbutton = display.newImage (“backbutton.png”)
backbutton.x = 60
backbutton.y = 280
backbutton.xScale = .4
backbutton.yScale = .4
localGroup:insert(backbutton)
local function touchedBackbutton (event)
if (“ended” == event.phase) then
director:changeScene (“screen5”)
media.playEventSound( “click_x.caf” )
end
end

backbutton:addEventListener (“touch”, touchedBackbutton)
–>This places the backbutton


– General event handler for fields

– You could also assign different handlers for each textfield

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

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard

– Hide keyboard
native.setKeyboardFocus( nil )
end

end

– Predefine local objects for use later
local defaultField, numberField, phoneField, urlField, emailField, passwordField
local fields = display.newGroup()

– *** Create native input textfields ***

– Note: currently this feature works in device builds or Xcode simulator builds only

– Note: currently this feature works in device builds only
local isAndroid = “Android” == system.getInfo(“platformName”)
local inputFontSize = 40
local inputFontHeight = 40
tHeight = 60

if isAndroid then
– Android text fields have more chrome. It’s either make them bigger, or make the font smaller.
– We’ll do both
inputFontSize = 40
inputFontHeight = 40
tHeight = 60
end
numberField = native.newTextField( 240, 145, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “number”

numberField = native.newTextField( 240, 200, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “number”

– Add fields to our new group

fields:insert(numberField)

________________________________________________________________



– Create a Background touch event

local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bkgd:setFillColor( 0, 0, 0, 0 ) – set Alpha = 0 so it doesn’t cover up our buttons/fields

– Tapping screen dismisses the keyboard

– Needed for the Number and Phone textFields since there is
– no return key to clear focus.

local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end

– Determine if running on Corona Simulator

local isSimulator = “simulator” == system.getInfo(“environment”)

– Native Text Fields not supported on Simulator

if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, “Verdana-Bold”, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end

– Add listener to background for user “tap”
bkgd:addEventListener( “tap”, listener )

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment
–REMOVE THE FOLLOWING LINE:

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13427 reply_id: 313427[/import]

Okay I got this fixed but do have another question.

I have my 3 text fields on the screen now and when I put them on my device they show up. What coding do I need to make the fields interact with each other? I basically want the top box to subtract the lower box and enter the result in the 3rd field. Now that I’m thinking about it, the 3rd field probably shouldn’t be a text field since the person is not going to enter anything there. They are just going to see a result there. This is the coding I have.

[lua]numberField = native.newTextField( 180, 125, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “number”

numberField = native.newTextField( 180, 180, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “number”

numberField = native.newTextField( 180, 245, 120, tHeight, fieldHandler )
numberField.font = native.newFont( native.systemFontBold, inputFontSize )
numberField.inputType = “number”

– Add fields to our new group

fields:insert(numberField)[/lua] [import]uid: 72372 topic_id: 13427 reply_id: 49356[/import]

This was solved in your other thread, correct? (Just making sure it really was solved and you don’t need more info ;)) [import]uid: 52491 topic_id: 13427 reply_id: 49448[/import]

Hey Peach,

no unless I am doing something wrong. I tried the information that was given and changed the names but it is given me some type of Director error. This is what I wrote. I’ll put it in the other area to keep it in the same place also.

[lua]numberFieldTwo:addEventListener(“onChange”, textFunction)

function textFunction()
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end[/lua] [import]uid: 72372 topic_id: 13427 reply_id: 49450[/import]

Is the director error the ONLY error being shown? [import]uid: 52491 topic_id: 13427 reply_id: 49742[/import]

Yes the director error was the only error I was showing but I fixed that now. I am now having the problem with the subtracting not working. My screen shows and I can enter the numbers but it does not subtract or do anything. It justs lets me enter the numbers.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local physics = require (“physics”)
physics.start ()

local ui = require(“ui”)

local tHeight – forward reference

local background = display.newImage (“blackbackground.png”)
background.x = 240
localGroup:insert(background)
–> This sets the background

local screen6 = display.newImage (“screen6.png”)
screen6.x = 240
localGroup:insert(screen6)
–> This sets Screen6


– General event handler for fields

– You could also assign different handlers for each textfield

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

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key (if available) on the onscreen keyboard

– Hide keyboard
native.setKeyboardFocus( nil )
end

end

– Predefine local objects for use later
local numberField, numberFieldTwo, numberFieldThree, numberFieldFour, difference, textFunction
local fields = display.newGroup()


– Create a Background touch event

– Tapping screen dismisses the keyboard

– Needed for the Number textField since there is
– no return key to clear focus.

local listener = function( event )
– Hide keyboard
print(“tap pressed”)
native.setKeyboardFocus( nil )
end

– Determine if running on Corona Simulator

local isSimulator = “simulator” == system.getInfo(“environment”)

– Native Text Fields not supported on Simulator

if isSimulator then
msg = display.newText( “Native Text Fields not supported on Simulator!”, 0, 280, “Verdana-Bold”, 12 )
msg.x = display.contentWidth/2 – center title
msg:setTextColor( 255,255,0 )
end

– Add listener to background for user “tap”
screen6:addEventListener( “tap”, listener )

local house = display.newImage (“house.png”)
house.x = 45
house.y = 35
house.xScale = .5
house.yScale = .5
localGroup:insert(house)
–>This places the House

local function touchedHouse (event)
if (“ended” == event.phase) then
director:changeScene (“titlescreen”)
media.playEventSound( “click_x.caf” )
end
end

house:addEventListener (“touch”, touchedHouse)

– Handlers for login dialog UI

local function onnumberFieldTwo( event )
if ( “began” == event.phase ) then
– Note: this is the “keyboard appearing” event
– In some cases you may want to adjust the interface while the keyboard is open.

elseif ( “ended” == event.phase ) then
– Automatically tab to numberFieldThree if user taps screen (convenient!)
native.setKeyboardFocus( numberFieldThree )

numberFieldTwo:addEventListener(“onChange”, textFunction)

end
end

local function onnumberFieldThree( event )
– Hide keyboard when the user taps the screen
if ( “ended” == event.phase ) then
native.setKeyboardFocus( nil )
end

end

local function onnumberFieldFour( event )
– Hide keyboard when the user taps the screen
if ( “ended” == event.phase ) then
native.setKeyboardFocus( nil )

local function textFunction()
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text

end

end
end

local smallbrain = display.newImage (“smallbrain.png”)
smallbrain.x = 425
smallbrain.y = 275
smallbrain.xScale = .5
smallbrain.yScale = .5
localGroup:insert(smallbrain)
–>This places the brain

local function touchedSmallbrain (event)
if (“ended” == event.phase) then
director:changeScene (“screen7a”)
media.playEventSound( “click_x.caf” )
end
end

smallbrain:addEventListener (“touch”, touchedSmallbrain)

local backbutton = display.newImage (“backbutton.png”)
backbutton.x = 60
backbutton.y = 280
backbutton.xScale = .4
backbutton.yScale = .4
localGroup:insert(backbutton)

local function touchedBackbutton (event)
if (“ended” == event.phase) then
director:changeScene (“screen5”)
media.playEventSound( “click_x.caf” )
end
end

backbutton:addEventListener (“touch”, touchedBackbutton)
–>This places the backbutton


– *** Create native input textfields ***

– Note: currently this feature works in device builds or Xcode simulator builds only

– Note: currently this feature works in device builds only
local isAndroid = “Android” == system.getInfo(“platformName”)
local inputFontSize = 30
local inputFontHeight = 30
tHeight = 50

if isAndroid then
– Android text fields have more chrome. It’s either make them bigger, or make the font smaller.
– We’ll do both
inputFontSize = 30
inputFontHeight = 30
tHeight = 50
end

numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, fieldHandler )
numberFieldTwo.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldTwo.inputType = “number”

numberFieldThree = native.newTextField( 180, 180, 120, tHeight, fieldHandler )
numberFieldThree.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldThree.inputType = “number”

numberFieldFour = native.newTextField( 180, 245, 120, tHeight, fieldHandler )
numberFieldFour.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldFour.inputType = “number”

– Add fields to our new group

fields:insert(numberFieldTwo)
fields:insert(numberFieldThree)
fields:insert(numberFieldFour)

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13427 reply_id: 49759[/import]

Hey,

I can’t see where in your code you are actually trying to do your subtraction; what lines? [import]uid: 52491 topic_id: 13427 reply_id: 49873[/import]

Peach,

I tried on line 134. You can look at my posting for text fields also. [import]uid: 72372 topic_id: 13427 reply_id: 49894[/import]

Take a look at this one…
https://developer.anscamobile.com/forum/2011/08/08/text-field

[import]uid: 72372 topic_id: 13427 reply_id: 49899[/import]

Hmmmm, I made a template recently that I sell on Techority that is a similar idea to this, only it’s addition and not subtraction.

When I did that, I needed to use tonumber()

Could you give that a go for the math part? It might still be seeing the input as strings. [import]uid: 52491 topic_id: 13427 reply_id: 50147[/import]