textfield text value

I've got some doubts about nexTextField:

how can I read the .text value?

I am trying this from the listener function but it doens't seem to work:
if (searchbox.text == "type something") then...

is there anyway to tweak the borders?

thanks

OK. got the value.

But I find the phase == "ended" doens't trigger when I tap another element outside the textfield.

What's the right way of handling it?

elseif ( "ended" == event.phase ) then
searchbox.text = "ENDED"
native.setKeyboardFocus( nil )

thanks again

You have to add a touch event outside the text field so when it's touched the touch listener calls native.setKeyboardFocus(nil). That should fire your text field listener.

-Tom

Requesting a working example that does what seems like a very common need in my world -

- a native text input field

- a button

- passing the text to a function

The Sample Code has Native Keyboard which demonstrates buttons an native text - but the buttons remove or add the text input fields instead of the more ordinary usage I explain above. And I've tried unsuccessfully to make it happen :(

The Sample Code Button Events demonstrates buttons to functions - and I indeed can get my function to run when a button is released! wow the rush of small victories after days of reading and puzzling. It's like the good feeling after stopping hitting one's head on the wall.

But that still doesn't give me a text input that when the button is pressed - passes the text to my function. I'll keep trying... but I highly advise for the sake of newbies that you make one simple sample that actually does this.

Thanks,

- April

PS Also suggest including in the working sample code how to pre-fill the text input field

Hi April,

just to confirm what you want.

On the screen there is a native text input field plus a button. When do you want the keyboard to appear?
Right away?
That button... when you press it... you want the content of the text input field to be used as a parameter for a function call?

I am asking so I can create a sample for you.

Cheers
Michael Hartlef

http://www.whiteskygames.com
http://www.twitter.com/mhartlef

Here is come demo code that will do most of what you want. It pre-fills the text field and when the user presses enter, it looks to see if text has changed and displays it in an alert box. I'm sure Mike will come up with a better sample.

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
local tfield                    -- forward reference
 
-- Handle the text field
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 )
                
                -- Did the text field change?
                if tField.text ~= "Name?" then
                        native.showAlert( "Text Field Entered", tField.text )
                end
                        
        end
 
end
 
-- Create Text Field and prefill the field
--
tField = native.newTextField( 10, 30, 180, 30, fieldHandler )
tField.font = native.newFont( native.systemFontBold, 18 )
tField.text = "Name?"

Hi Mike,

Keyboard should appear if user taps within the field.

The user may instead if they like the pre-filled content of the field, just press the button.

Yes, the content of the text input field is used as a parameter for a function call.

The above is the way I will use it... but if there is something more generic that all new users would relate to that's fine. Such as keyboard appears instantly and there's no prefilled text.

Then maybe a note about how to pre-fill the text. The way Tom shows it looks very easy but being undocumented so far as I can tell... I was not smart enough to just try various things that might work :) - or not fully understanding the language yet.

There's a nice feature in the NativeKeyboard2 example which will make the keyboard disappear if you tap a blank area of the background.

I did finally get my app working by taking NativeKeyboard2 example and then removing a lot of code and stuffing things in. I doubt that I need the ui.lua or some of the other specialized code.

Thanks very much to both Mike and Tom and I hope the examples make it into an area where new users can see it easily.

- April

I can't seem to get this to work. I can't pass the value. Could anyone check this code?

Thanks

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
-- Handle the text field
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
                jumpscene.stride = numberField.text
                -- Hide keyboard
                native.setKeyboardFocus( nil )
                
                -- Did the text field change?
                if numberField.text ~= "Name?" then
                        native.showAlert( "Text Field Entered", numberField.text )
                end
                        
        end
 
end
/<code>

Is there any way to input real numbers with the number keyboard?

I entered the above code example and I can't get my "return" key to work on the iPad 1. Is this a glitch? If not, please help! Thanks.

I figured out my problem. I was putting the fileHandler(event) below the text field, but I had made it a global function so I don't understand why it wasn't working.

views:2185 update:2011/10/5 8:48:05
corona forums © 2003-2011