package com.preet.keyboardhandler; import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; import com.google.appinventor.components.annotations.DesignerComponent; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.annotations.SimpleObject; import com.google.appinventor.components.annotations.SimpleProperty; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.AndroidViewComponent; import com.google.appinventor.components.runtime.ComponentContainer; @DesignerComponent( category = ComponentCategory.EXTENSION, description = "A Non-visible component that helps you to handle the keyboard of TextBox or PasswordTextBox component.", nonVisible = true, iconName = "", version = 1 ) @SimpleObject(external = true) public class KeyboardHandler extends AndroidNonvisibleComponent { private final Context context; public KeyboardHandler(ComponentContainer container) { super(container.$form()); this.context = container.$context(); } @SimpleProperty(description = "Flag for HideKeyboard to indicate that the soft input window should only be hidden if " + "it was not explicitly shown by the user.") public int HideImplicitOnly() { return InputMethodManager.HIDE_IMPLICIT_ONLY; } @SimpleProperty(description = "Flag for HideKeyboard to indicate that the soft input window should normally be hidden, " + "unless it was originally shown with SHOW_FORCED.") public int HideNotAlways() { return InputMethodManager.HIDE_NOT_ALWAYS; } @SimpleProperty(description = "Flag for HideKeyboard and ShowKeyboard, the state " + "of the soft input window changed from shown to hidden.") public int ResultHidden() { return InputMethodManager.RESULT_HIDDEN; } @SimpleProperty(description = "Flag for HideKeyboard and ShowKeyboard, the state " + "of the soft input window changed from hidden to shown.") public int ResultShown() { return InputMethodManager.RESULT_SHOWN; } @SimpleProperty(description = "Flag for HideKeyboard and ShowKeyboard, the state " + "of the soft input window was unchanged and remains hidden.") public int ResultUnchangedHidden() { return InputMethodManager.RESULT_UNCHANGED_HIDDEN; } @SimpleProperty(description = "Flag for HideKeyboard and ShowKeyboard, the state " + "of the soft input window was unchanged and remains shown.") public int ResultUnchangedShown() { return InputMethodManager.RESULT_UNCHANGED_SHOWN; } @SimpleProperty(description = "Flag for ShowKeyboard to indicate that the user has forced the input method open " + "(such as by long-pressing menu) so it should not be closed until they explicitly do so.") public int ShownForced() { return InputMethodManager.SHOW_FORCED; } @SimpleProperty(description = "Flag for ShowKeyboard to indicate that this is an implicit request to show the input " + "window, not as the result of a direct request by the user. The window may not be shown in this case.") public int ShownImplicit() { return InputMethodManager.SHOW_IMPLICIT; } @SimpleFunction(description = "A function to hide the keyboard of given textBox.") public void HideKeyboard(final AndroidViewComponent textBox, final int flag) { View view = (View) textBox.getView(); InputMethodManager inputMethodManager = (InputMethodManager) this.context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), flag); } @SimpleFunction(description = "A function to show the keyboard of given textBox.") public void ShowKeyboard(final AndroidViewComponent textBox, final int flag) { View view = (View) textBox.getView(); InputMethodManager inputMethodManager = (InputMethodManager) this.context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(view, flag); } }