diff --git a/ui/editable-text-base/editable-text-base.android.ts b/ui/editable-text-base/editable-text-base.android.ts index 657a8c27c..522c11118 100644 --- a/ui/editable-text-base/editable-text-base.android.ts +++ b/ui/editable-text-base/editable-text-base.android.ts @@ -2,13 +2,13 @@ import textBase = require("ui/text-base"); import dependencyObservable = require("ui/core/dependency-observable"); import enums = require("ui/enums"); +import utils = require("utils/utils"); export class EditableTextBase extends common.EditableTextBase { private _android: android.widget.EditText; /* tslint:disable */ private _dirtyTextAccumulator: string; /* tslint:enable */ - private _imm: android.view.inputmethod.InputMethodManager; constructor(options?: textBase.Options) { super(options); @@ -18,9 +18,7 @@ export class EditableTextBase extends common.EditableTextBase { return this._android; } - public _createUI() { - this._imm = this._context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE); - + public _createUI() { this._android = new android.widget.EditText(this._context); this._configureEditText(); this.android.setTag(this.android.getKeyListener()); @@ -103,23 +101,20 @@ export class EditableTextBase extends common.EditableTextBase { } public _onDetached(force?: boolean) { - this._imm = undefined; this._android = undefined; super._onDetached(force); } public dismissSoftInput() { - if (this._imm) { - this._imm.hideSoftInputFromWindow(this._android.getWindowToken(), 0); - } + utils.ad.dismissSoftInput(this._nativeView); } public focus(): boolean { var result = super.focus(); - if (result && this._nativeView) { - this._imm.showSoftInput(this._nativeView, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT); + if (result) { + utils.ad.showSoftInput(this._nativeView); } return result; diff --git a/ui/search-bar/search-bar-common.ts b/ui/search-bar/search-bar-common.ts index 2252c4b6e..adf458b9c 100644 --- a/ui/search-bar/search-bar-common.ts +++ b/ui/search-bar/search-bar-common.ts @@ -50,4 +50,7 @@ export class SearchBar extends view.View implements definition.SearchBar { value instanceof color.Color ? value : new color.Color(value)); } + public dismissSoftInput() { + // + } } \ No newline at end of file diff --git a/ui/search-bar/search-bar.android.ts b/ui/search-bar/search-bar.android.ts index 4012c803e..e54dc94c2 100644 --- a/ui/search-bar/search-bar.android.ts +++ b/ui/search-bar/search-bar.android.ts @@ -96,6 +96,20 @@ global.moduleMerge(common, exports); export class SearchBar extends common.SearchBar { private _android: android.widget.SearchView; + public dismissSoftInput() { + utils.ad.dismissSoftInput(this._nativeView); + } + + public focus(): boolean { + var result = super.focus(); + + if (result) { + utils.ad.showSoftInput(this._nativeView); + } + + return result; + } + public _createUI() { this._android = new android.widget.SearchView(this._context); diff --git a/ui/search-bar/search-bar.d.ts b/ui/search-bar/search-bar.d.ts index 087392fd7..73e272e39 100644 --- a/ui/search-bar/search-bar.d.ts +++ b/ui/search-bar/search-bar.d.ts @@ -78,5 +78,10 @@ declare module "ui/search-bar" { * Raised when a search bar search is closed. */ on(event: "close", callback: (args: observable.EventData) => void, thisArg?: any); + + /** + * Hides the soft input method, ususally a soft keyboard. + */ + dismissSoftInput(): void; } } \ No newline at end of file diff --git a/ui/search-bar/search-bar.ios.ts b/ui/search-bar/search-bar.ios.ts index b7c648198..e785f9278 100644 --- a/ui/search-bar/search-bar.ios.ts +++ b/ui/search-bar/search-bar.ios.ts @@ -115,6 +115,10 @@ export class SearchBar extends common.SearchBar { super.onUnloaded(); } + public dismissSoftInput() { + (this.ios).resignFirstResponder(); + } + get ios(): UISearchBar { return this._ios; } diff --git a/utils/utils.android.ts b/utils/utils.android.ts index 54b3fb1b3..ae2a273c6 100644 --- a/utils/utils.android.ts +++ b/utils/utils.android.ts @@ -51,6 +51,28 @@ export module ad { export function getApplication() { return (com.tns).NativeScriptApplication.getInstance(); } export function getApplicationContext() { return getApplication().getApplicationContext(); } + var inputMethodManager: android.view.inputmethod.InputMethodManager; + export function getInputMethodManager() { + if (!inputMethodManager) { + inputMethodManager = getApplicationContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE); + } + return inputMethodManager; + } + + export function showSoftInput(nativeView: android.view.View) : void { + var imm = getInputMethodManager(); + if (imm && nativeView instanceof android.view.View) { + imm.showSoftInput(nativeView, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT); + } + } + + export function dismissSoftInput(nativeView: android.view.View): void { + var imm = getInputMethodManager(); + if (imm && nativeView instanceof android.view.View) { + imm.hideSoftInputFromWindow(nativeView.getWindowToken(), 0); + } + } + export module collections { export function stringArrayToStringSet(str: string[]): any { var hashSet = new java.util.HashSet(); diff --git a/utils/utils.d.ts b/utils/utils.d.ts index 97b923e5b..48b4e09d6 100644 --- a/utils/utils.d.ts +++ b/utils/utils.d.ts @@ -66,6 +66,21 @@ */ export function getApplicationContext(): android.content.Context; + /** + * Gets the native Android input method manager. + */ + export function getInputMethodManager(): android.view.inputmethod.InputMethodManager; + + /** + * Hides the soft input method, ususally a soft keyboard. + */ + export function dismissSoftInput(nativeView: android.view.View): void; + + /** + * Shows the soft input method, ususally a soft keyboard. + */ + export function showSoftInput(nativeView: android.view.View): void; + /** * Utility module dealing with some android collections. */