首页 > 解决方案 > 用于 Nativescript-Vue 的搜索栏的 DismissSoftInput()

问题描述

解释问题:

搜索栏无法关闭打开的键盘。这使得搜索栏非常不可用,因为正常的用户模式是用户搜索某些内容,然后按下该项目并在那里导航。在 Android 上(至少在 >= 5.x 上),打开的键盘将继续保持打开状态,即使在新页面上也是如此。

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

更新:

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

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

标签: nativescriptnativescript-vue

解决方案


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

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

更新:

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

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();
        }
    }
}

游乐场样本


推荐阅读