在Android上的NativeScript中,如何防止SearchBar专注于页面加载?

时间:2016-03-08 13:05:16

标签: nativescript

我在ScrollView中有一个SeachBar。在iOS中一切都很好。在Android上,ScrollView会自动滚动到SearchBar,为其添加焦点并显示软键盘。我可以通过在AndroidManifest.xml文件中添加android:windowSoftInputMode="stateHidden"作为活动来隐藏软键盘,但我无法确定如何防止焦点(因此自动滚动)。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:9)

使用 Angular2

应用程序/ my.component.html

<StackLayout (loaded)="onSearchLayoutLoaded($event)">
    <SearchBar hint="search here" (loaded)="onSearchBarLoaded($event)">
    </SearchBar>
</StackLayout>

应用程序/ my.component.ts

    onSearchLayoutLoaded(event) {
        if (event.object.android) {
            event.object.android.setFocusableInTouchMode(true);
        }
    }

    onSearchBarLoaded(event) {
        if (event.object.android) {
            event.object.android.clearFocus();
        }
    }

这消除了不必要地使用template reference variables

答案 1 :(得分:8)

对于Android,您需要做一些事情。如果您使用的是原生Android布局,请说LinerLayout,您可以设置android:focusableInTouchMode="true",这应该适用于大多数用例。因此,使用NativeScript,您将在 SearchBar 的父级上使用关联的方法,然后在搜索栏上调用`clearFocus()。

实施例

function removeSearchFocus() {
    // get the parent of the searchbar
    var parent = page.getViewById('parentLayout');
    var searchBar = page.getViewById('mySearchBar');
    if (parent.android) {
        parent.android.setFocusableInTouchMode(true);
        parent.android.setFocusable(true);
        searchBar.android.clearFocus();
    }
}

然后将此功能附加到其中一个页面导航事件,或将重要部分转储到您应用中可能已有的当前页面事件中。只需为父母分配一个ID和 SearchBar ,这样你就可以通过ID获取它,这应该可行。

答案 2 :(得分:0)

如何在加载的页面或加载事件中添加endEditing

searchBar = page.getViewById('your-searchbar');
searchBar.ios.endEditing(true);

或将焦点设置到您想要焦点的另一个元素,例如

somethingElse = page.getViewById('your-top-bar');
somethingElse.focus();