Forum Archive

Switch to next text field and update text with the same buttons

image-tech-nick

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

cvp

@image-tech-nick Sorry if I don't understand correctly.
Do you have made a Pythonista keyboard with digits buttons and a next field button?
And do you use it in a normal script having two TextFields?

Or do you have a normal script with the 10 digits buttons and a next field button?

cvp

As your input_num def refers to sender.superview['textfield name'], I suppose that all ui objects are in the same script, thus you don't use a Pythonista keyboard.
When you say that a TextField is active, that will say you have put the cursor on it.
But as soon you press a digit button, the TextField would no more be active.

image-tech-nick

With buttons I made a ui number pad 0-9. with the block of code above when you press the buttons it adds numbers to text field object spd_tf. I want to change to the second text field object sze_tf. Then the user can add numbers to sze_tf.

cvp

@image-tech-nick I understand that but why do you use TextFields if you don't type in them with a normal keyboard but with your special digits buttons?

image-tech-nick

Is there a way to write a function that checks what text field has the curser?

cvp

Try this, quick and dirty as usual,

import ui
mv = ui.View()
mv.background_color = 'white'
def num_inp(sender):
    if sender.title == 'next field':
        sender.active = 1 - sender.active
        return
    tf_idx = sender.superview['nf'].active
    tf = sender.superview[['t1','t2'][tf_idx]]
    tf.text += sender.title

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() 
image-tech-nick

Or maybe I could have a while loop based on TextField.delegate conditions. It could break out of the loop with the "Next Field" button, hmmm!

cvp

@image-tech-nick said:

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.

image-tech-nick

@cvp Thank you so much, that worked perfectly.

cvp

@image-tech-nick to see the cursor,
Add this line at end of the script

t1.begin_editing() 

And this line at end of the num_inp def

    tf.begin_editing() 

But, so, the keyboard appears...it is a choice that you have to do.
Or draw yourself a kind of cursor...

cvp

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()
mikael

@image-tech-nick, just to be sure, did you consider using one of the iOS keyboard types like ui.KEYBOARD_DECIMAL_PAD? (Set with TextField.keyboard_type.)

cvp

@mikael you're right but don't forget that his main question is relative to a "next field"

mikael

@image-tech-nick, also, for usability and depending on your use case, you could consider using ui.KEYBOARD_NUMBERS which has the enter key, and then have a delegate with textfield_did_end_editing method to do the jump to the next text field.

cvp

@image-tech-nick or see here to use keyboard like