codelv / enaml-native

Build native mobile apps in python with enaml
https://codelv.com/projects/enaml-native/
MIT License
259 stars 21 forks source link

API to Hide/Close Android Keyboard #48

Closed MaxMorais closed 6 years ago

MaxMorais commented 7 years ago

After click on any EditText widget the SoftKeyboard still open in all other screens! Will be good if we can Disable or Hide, the soft Keyboard for certain situation, to take the control of when we need, or we dont need the keyboard to still open.

Some tip: https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard

MaxMorais commented 7 years ago

code from @frmdstryr via Gitter

from enamlnative.core.api import *
from enamlnative.widgets.api import *

from enamlnative.android.app import AndroidApplication
from enamlnative.android.bridge import JavaBridgeObject, JavaMethod, set_default

class InputMethodManager(JavaBridgeObject):
    __nativeclass__ = set_default('android.view.inputmethod.InputMethodManager')
    toggleSoftInput = JavaMethod('int', 'int')
    HIDE_IMPLICIT_ONLY = 1

def toggle_keyboard():
    app = AndroidApplication.instance()
    activity = app.widget
    def on_ready(__id__):
        ims = InputMethodManager(__id__=__id__)
        ims.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    activity.getSystemService(activity.INPUT_METHOD_SERVICE).then(on_ready)

enamldef ContentView(Flexbox):
    flex_direction = "column"
    Button:
        text = "Click to toggle the keyboard"
        clicked :: toggle_keyboard()
frmdstryr commented 7 years ago

Should be able to pip install enaml-native==2.11.9 then build-python and rerun.

See https://github.com/codelv/enaml-native/blob/master/examples/keyboard.enaml

If it works, please close the ticket. Thanks!

frmdstryr commented 6 years ago

This is working with others so closing due to inactivity.