Adding and removing native text fields objects.
Changing button labels on the flyable.
Tapping the background to dismiss the native keyboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | local ui = require("ui") ------------------------------------------- -- 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() ------------------------------------------- -- *** Buttons Presses *** ------------------------------------------- -- Default Button Pressed local defaultButtonPress = function( event ) -- Make sure display object still exists before removing it if defaultField then print("Default button pressed ... removing textField") -- defaultField:removeSelf() fields:remove( defaultField ) defaultButton:setText( "Add Default textField" ) defaultField = nil -- do this so we don't remove it a second time else -- Add the text field back again defaultField = native.newTextField( 10, 30, 180, 30, fieldHandler ) defaultField.font = native.newFont( native.systemFontBold, 18 ) fields:insert(defaultField) defaultButton:setText( "Remove Default textField" ) end end -- Number Button Pressed local numberButtonPress = function( event ) print("Number button pressed ... removing textField") -- Make sure display object still exists before removing it if numberField then numberField:removeSelf() numberButton:setText( "Add Number textField" ) numberField = nil -- do this so we don't remove it a second time else -- Add the text field back again numberField = native.newTextField( 10, 70, 180, 30, fieldHandler ) numberField.font = native.newFont( native.systemFontBold, 18 ) numberField.inputType = "number" fields:insert(numberField) numberButton:setText( "Remove Number textField" ) end end ------------------------------------------- -- *** 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 = 18 local inputFontHeight = 30 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 = 14 inputFontHeight = 42 end defaultField = native.newTextField( 10, 30, 180, 30, fieldHandler ) defaultField.font = native.newFont( native.systemFontBold, inputFontSize ) numberField = native.newTextField( 10, 70, 180, 30, fieldHandler ) numberField.font = native.newFont( native.systemFontBold, inputFontSize ) numberField.inputType = "number" phoneField = native.newTextField( 10, 110, 180, 30, fieldHandler ) phoneField.font = native.newFont( native.systemFontBold, inputFontSize ) phoneField.inputType = "phone" urlField = native.newTextField( 10, 150, 180, 30, fieldHandler ) urlField.font = native.newFont( native.systemFontBold, inputFontSize ) urlField.inputType = "url" emailField = native.newTextField( 10, 190, 180, 30, fieldHandler ) emailField.font = native.newFont( native.systemFontBold, inputFontSize ) emailField.inputType = "email" passwordField = native.newTextField( 10, 230, 180, 30, fieldHandler ) passwordField.font = native.newFont( native.systemFontBold, inputFontSize ) passwordField.isSecure = true -- Add fields to our new group fields:insert(defaultField) fields:insert(numberField) ------------------------------------------- -- *** Add field labels *** ------------------------------------------- local defaultLabel = display.newText( "Default", 200, 35, native.systemFont, 18 ) defaultLabel:setTextColor( 170, 170, 255, 255 ) local defaultLabel = display.newText( "Number", 200, 75, native.systemFont, 18 ) defaultLabel:setTextColor( 255, 150, 180, 255 ) local defaultLabel = display.newText( "Phone", 200, 115, native.systemFont, 18 ) defaultLabel:setTextColor( 255, 220, 120, 255 ) local defaultLabel = display.newText( "URL", 200, 155, native.systemFont, 18 ) defaultLabel:setTextColor( 170, 255, 170, 255 ) local defaultLabel = display.newText( "Email", 200, 195, native.systemFont, 18 ) defaultLabel:setTextColor( 120, 255, 245, 255 ) local defaultLabel = display.newText( "Password", 200, 235, native.systemFont, 18 ) defaultLabel:setTextColor( 255, 235, 170, 255 ) ------------------------------------------- -- *** Create Buttons *** ------------------------------------------- -- "Remove Default" Button defaultButton = ui.newButton{ default = "buttonBlue.png", over = "buttonBlueOver.png", onPress = defaultButtonPress, text = "Remove Default textField", emboss = true } -- "Remove Number" Button numberButton = ui.newButton{ default = "buttonBlue.png", over = "buttonBlueOver.png", onPress = numberButtonPress, text = "Remove Number textField", emboss = true } -- Position the buttons on screen local s = display.getCurrentStage() defaultButton.x = s.stageWidth/2; defaultButton.y = 365 numberButton.x = s.stageWidth/2; numberButton.y = 430 ------------------------------------------- -- 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 ) |