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

Rae*_*rst 5 nativescript

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

Eri*_*wer 9

使用Angular2:

应用程序/ my.component.html

<StackLayout (loaded)="onSearchLayoutLoaded($event)">
    <SearchBar hint="search here" (loaded)="onSearchBarLoaded($event)">
    </SearchBar>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)

应用程序/ my.component.ts

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

    onSearchBarLoaded(event) {
        if (event.object.android) {
            event.object.android.clearFocus();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这消除了不必要地使用模板引用变量.


Bra*_*tin 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Emi*_*erg 0

endEditing向页面加载或加载事件添加一个怎么样?

searchBar = page.getViewById('your-searchbar');
searchBar.ios.endEditing(true);
Run Code Online (Sandbox Code Playgroud)

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

somethingElse = page.getViewById('your-top-bar');
somethingElse.focus();
Run Code Online (Sandbox Code Playgroud)