native.setKeyboardFocus

I was reading the APIs but I had trouble fully understanding it. My problem is that when you touch the textfields located near the bottom of the screen, the keyboard appears on the device and actually covers the textfield you chose to edit. This leaves me blindly typing something into the textfield. I’m thinking native.setKeyboardFocus will “focus” on the textfield I’m interested in and fix the problem but I’m also not sure if by focus they mean that the vertical flashing line (sorry I don’t know the proper name) will go to another textfield automatically upon “ended” phase. At least this is how they used it in the API example.

http://docs.coronalabs.com/api/library/native/setKeyboardFocus.html
When I try it in the simulator, I have multiple textFields going to a single listener function and I’m trying to focus the on the textfield that was touched.
[lua] local function boxHandler (event)

if ( “began” == event.phase ) then

native.setKeyboardFocus( event.target )
print (event.target)

event.target.text = “”

elseif ( “submitted” == event.phase ) then

– Hide keyboard
native.setKeyboardFocus( nil )
end

end[/lua]

and the terminal gives me this.

[lua] 2012-11-20 17:55:43.794 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.797 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.801 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.806 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.829 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.845 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.849 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.864 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.871 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.897 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.912 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.915 Corona Simulator[4244:903] table: 0x11aa471f0
2012-11-20 17:55:43.929 Corona Simulator[4244:903] table: 0x11aa471f0[/lua]

Can anyone help?

Thanks in advance!
[import]uid: 35535 topic_id: 33196 reply_id: 333196[/import]

That’s expected behavior.

What are you hoping for it to print? If you want to know which is being touched you might give them names, eg;

box1.name = “box1”

Then print event.target.name - etc. [import]uid: 52491 topic_id: 33196 reply_id: 131880[/import]

Okay I’ll keep that in mind as well but I was more hoping for clarification on the keyboard focus :slight_smile: [import]uid: 35535 topic_id: 33196 reply_id: 131883[/import]

What you have to do is move your elements up the screen. How you do that will vary greatly with the app in question. You can move the whole form up at once if the whole form fits on the screen with the keyboard. If it’s a really long form, you will have to put code to maybe slide up the form by one field each time a new field takes focus.

This can be as simple as when you set the keyboard focus to true, you use a transition.to() on the group holding your UI elements. When you dismiss the keyboard, use a transition.to() to move it back. But it could be as complex as trying to make sure in the keyboard handler you see when a new field gets a start editing phase and move it up X number of pixels and then when the keyboard dismisses move it back. [import]uid: 19626 topic_id: 33196 reply_id: 131942[/import]

That’s expected behavior.

What are you hoping for it to print? If you want to know which is being touched you might give them names, eg;

box1.name = “box1”

Then print event.target.name - etc. [import]uid: 52491 topic_id: 33196 reply_id: 131880[/import]

Okay I’ll keep that in mind as well but I was more hoping for clarification on the keyboard focus :slight_smile: [import]uid: 35535 topic_id: 33196 reply_id: 131883[/import]

What you have to do is move your elements up the screen. How you do that will vary greatly with the app in question. You can move the whole form up at once if the whole form fits on the screen with the keyboard. If it’s a really long form, you will have to put code to maybe slide up the form by one field each time a new field takes focus.

This can be as simple as when you set the keyboard focus to true, you use a transition.to() on the group holding your UI elements. When you dismiss the keyboard, use a transition.to() to move it back. But it could be as complex as trying to make sure in the keyboard handler you see when a new field gets a start editing phase and move it up X number of pixels and then when the keyboard dismisses move it back. [import]uid: 19626 topic_id: 33196 reply_id: 131942[/import]

Thanks! I’m using the transition.to and that seems to work well. Is there a common method to put the object back exactly where it came from? Or should I just experiment and play around with it.
**Edit**

I’m adding an extension to my object with the old x and y coordinates and that seems to work well at the moment [import]uid: 35535 topic_id: 33196 reply_id: 132214[/import]

Thanks! I’m using the transition.to and that seems to work well. Is there a common method to put the object back exactly where it came from? Or should I just experiment and play around with it.
**Edit**

I’m adding an extension to my object with the old x and y coordinates and that seems to work well at the moment [import]uid: 35535 topic_id: 33196 reply_id: 132214[/import]