native.newTextField read a number like -1.234

Hello.

This is  my first post. I´m from Portugal.

I´m also new at Corona. I´m just now trying some portions of code, to test what i can do… later make a more complex app.

Huge problem: How can ask the user, to input a number like for example -1.2344 …

I´m trying to convert to number from a 

defaultField1 = native.newTextField(  210, 200, 150, 30)

defaultField1 :addEventListener( “userInput”, textListener )

numValue = tonumber(defaultField1.text)

it seens tha the numValue is not workink, for example if later enter at textfield “10” and somewhere put a IF zz==10 don´t recognize as number 10 to compare…

Also tried to put .inputType = “number” but it opens numeric keybord but dont acept negative or decimal key, and this two keys are showned…

Is there someway to the user tap in a field, the program recognize the entered value, and store it in a variable??

Thanks.

You can’t compare numbers to strings.  For example 10 != “10”.  You need to convert both to numbers or strings and then compare them.

Hello “10” should be understood as a value entered in the text box…

Thanks

Is there someway, even alternative to nativetextfield to acept and open numeric only keyboard, that can acept “-” and “.” to compose a number variable for future math operations?

Thanks.

You can try the keyboard type of “decimal” to get numeric keyboard with a “.”.

Rob

Hello Rob, coding for Android gives you an error, syas something like is not supported…

Try “phone”

Rob

“Is there someway to the user tap in a field, the program recognize the entered value, and store it in a variable??”

Hi Jose,

Yes, you can gather that by reading “event.text” during the “began” phase of the userInput event (when the user selects the text field). See here for an example of how it might work:

https://docs.coronalabs.com/api/event/userInput/phase.html

I suggest you still use the default keyboard type (.inputType), since “phone” may only reveal phone keys like numbers, #, and maybe a couple other keys.

Brent

Hello.

I by now it works… i can achieve somthing to value if it is viable the app that i have in mind.

Important notes:

  1. variable xxx=native.newTextField, xxx should be declared as local at begining… if not any tries to convert to number will fail.

  2. “phone” type keyboard allows user to input, for example a “-1.23-2.1” well any char at any order… it could work if i can design some function to filter these user entries…

  3. input type as decimal in Android automatic changes to default when build the app.

Thanks.

You can’t compare numbers to strings.  For example 10 != “10”.  You need to convert both to numbers or strings and then compare them.

Hello “10” should be understood as a value entered in the text box…

Thanks

Is there someway, even alternative to nativetextfield to acept and open numeric only keyboard, that can acept “-” and “.” to compose a number variable for future math operations?

Thanks.

You can try the keyboard type of “decimal” to get numeric keyboard with a “.”.

Rob

Hello Rob, coding for Android gives you an error, syas something like is not supported…

Try “phone”

Rob

“Is there someway to the user tap in a field, the program recognize the entered value, and store it in a variable??”

Hi Jose,

Yes, you can gather that by reading “event.text” during the “began” phase of the userInput event (when the user selects the text field). See here for an example of how it might work:

https://docs.coronalabs.com/api/event/userInput/phase.html

I suggest you still use the default keyboard type (.inputType), since “phone” may only reveal phone keys like numbers, #, and maybe a couple other keys.

Brent

Hello.

I by now it works… i can achieve somthing to value if it is viable the app that i have in mind.

Important notes:

  1. variable xxx=native.newTextField, xxx should be declared as local at begining… if not any tries to convert to number will fail.

  2. “phone” type keyboard allows user to input, for example a “-1.23-2.1” well any char at any order… it could work if i can design some function to filter these user entries…

  3. input type as decimal in Android automatic changes to default when build the app.

Thanks.