Forum Archive

Control keyboard ui

FDT

Hi - I'm building a special calculator. I use textviews to enter and display data. I would like to have the keyboard disappear, after I press a UI button (my calculate button). When I enter data into a textview, the keyboard covers the lower half my calculator UI and it does not disappear, after I press the calculate button. Also, I'd like to have the keyboard pop up as a numeric keyboard, when I tap a textview., since I'm building calculators.

I use a buttons to call a functions. Do I have to make the keyboard control code part of the function(s) or should it be located at the end of the script?

Thanks,

Forrest

mikael

@FDT, use:

  • textfield.end_editing() to hide the keyboard when you do not need it
  • textfield.keyboard_type set to one of the following, experiment which is best for your use case:
  • ui.KEYBOARD_DECIMAL_PAD
  • ui.KEYBOARD_NUMBERS
  • ui.KEYBOARD_NUMBER_PAD
mikael

@FDT, I do not fully understand your last question. Maybe if you share some (simplified) code?

cvp

@FDT said:

I'd like to have the keyboard pop up as a numeric keyboard

If you really want a numeric keyboard only, see here

FDT

@mikael It would be great if you could show a little snippet of code showing the context of the methods.

FDT

@mikael said:

textfield.end_editing()

I got the textfield.end_editing() to work and it's a big help! The keyboard_type code has me stumped. I have googled it extensively and haven't found a syntax that works.

mikael

@FDT, mostly, do not google, but highlight the class name and select Help... from the popup options to see attributes like these.

Here’s a quick sample:

import ui

v = ui.View()
tf = ui.TextField(
    placeholder='First number',
    keyboard_type=ui.KEYBOARD_DECIMAL_PAD,
    width=200, height=40,
    center=v.bounds.center(), flex='RTBL',
)
v.add_subview(tf)
v.present()