用于 Nativescript-Vue 搜索栏的 DismissSoftInput()

DismissSoftInput() for search-bar for Nativescript-Vue

解释问题:

The search-bar has no way of dismissing an open keyboard. This makes the search-bar a quite unusable as the normal user pattern is that the user search for something and then press the item and gets navigated there. On Android (at least on >= 5.x), the open keyboard will continue to stay open, even on the new page.

参考 Github 上的问题,任何人如何为 Nativescript-Vue 而不是为带有 Typescript 的 Nativescript 做到这一点

已更新:

已添加游乐场:https://play.nativescript.org/?template=play-vue&id=hrrcW9

如果我最小化该应用程序,然后再次单击它,键盘会再次弹开。

正如您已经在链接的问题中看到的那样,功能请求已完成关闭。 dismissSoftInput() 现在是 SearchBar 上隐藏键盘的方法。

如果仍有问题,请分享您的代码。

更新:

Android 的默认行为是将第一个可聚焦元素聚焦在片段/activity 上。添加事件侦听器/超时以从每个屏幕上移除焦点可能很烦人,我更喜欢使用自动对焦视图作为布局的第一个元素(不会对屏幕设计产生任何影响),这会阻止自动对焦到我的文本字段/搜索栏。

import { View } from "tns-core-modules/ui/core/view";

export class AutoFocusView extends View {

    createNativeView() {
        if (typeof android !== "undefined") {
            const linearLayout = new android.widget.LinearLayout(this._context);
            linearLayout.setFocusableInTouchMode(true);
            linearLayout.setFocusable(true);
            return linearLayout;
        }
        return super.createNativeView();
    }

    onLoaded() {
        super.onLoaded();
        if (typeof android !== 'undefined') {
            this.requestFocus();
        }
    }

    requestFocus() {
        if (typeof android !== "undefined") {
            const nativeViewProtected = this.nativeViewProtected;
            nativeViewProtected.requestFocus();
        }
    }
}

Playground Sample