Switch to next text field and update text with the same buttons
I made a number keypad that works like a Pythonista calculator. Only difference is the I have two text fields. I added a "Next Field" button so you can switch to the next text field. How can when I switch text fields use the same number pad? I looked all through the wiki and don't see anything. There a way you check what text field is active for text input?
def num_input(sender): '@type sender: ui.Button' t = sender.title sze_tf = sender.superview['size_tf'] # 1st text field spd_tf = sender.superview['speed_tf'] # second text field if t in '0123456789': if spd_tf.text == 'avg. speed': spd_tf.text = t else: spd_tf.text += t elif t == '.' and spd_tf.text[-1] != '.': spd_tf.text += t elif t == 'DEL': spd_tf.text = spd_tf.text[:-1] else: spd_tf.text += t ```
Or maybe I could have a while loop based on
TextField.delegateconditions. It could break out of the loop with the "Next Field" button, hmmm!
is there a way to write a function that checks what text field has the curser?
As soon you press a digit button, the TextField looses its cursor...thus you have to store yourself where you want, a global, or like in my little script in an attribute, which TextField was active.
@cvp Thank you so much, that worked perfectly.
@image-tech-nick to see the cursor,
Add this line at end of the script
And this line at end of the num_inp def
But, so, the keyboard appears...it is a choice that you have to do.
Or draw yourself a kind of cursor...
This one is better
import ui mv = ui.View() mv.background_color = 'white' def num_inp(sender): if sender.title == 'next field': sender.active = 1 - sender.active tf_idx = sender.superview['nf'].active tf = sender.superview[['t1','t2'][tf_idx]] if sender.title != 'next field': tf.text += sender.title tf.begin_editing() t1 = ui.TextField(name='t1') t1.frame = (10,10,100,20) mv.add_subview(t1) t2 = ui.TextField(name='t2') t2.frame = (10,40,100,20) mv.add_subview(t2) for i in range(0,10): b = ui.Button() b.action = num_inp b.title = str(i) b.frame = (10,70+i*30,32,20) mv.add_subview(b) nf =ui.Button(name='nf') nf.title = 'next field' nf.action = num_inp nf.active = 0 nf.frame = (10,400,100,20) mv.add_subview(nf) mv.present() t1.begin_editing()
@image-tech-nick, just to be sure, did you consider using one of the iOS keyboard types like
ui.KEYBOARD_DECIMAL_PAD? (Set with
@mikael you're right but don't forget that his main question is relative to a "next field"
@image-tech-nick, also, for usability and depending on your use case, you could consider using
ui.KEYBOARD_NUMBERSwhich has the enter key, and then have a delegate with
textfield_did_end_editingmethod to do the jump to the next text field.