【发布时间】:2021-10-08 16:27:11
【问题描述】:
我的目标是拥有一个保持焦点的隐藏文本字段,以便它可以接收来自蓝牙条形码扫描仪的输入。到目前为止,我已经完成了这一点,但是软键盘总是显示,这对应用程序来说是一个巨大的破坏。我曾尝试使用dismissSoftInput(),但键盘会在它被关闭之前弹出一会儿。我需要永远不要出现。
现在,我的代码是
@ViewChild("scannerInput") scannerInput: ElementRef;
private _isRunning: boolean;
constructor() {}
ngOnInit(): void {
// as long as _isRunning is true, keepFocus will continue to call itself to make sure the input is focused
this._isRunning = true;
this.keepFocus();
}
ngOnDestroy(): void {
// when the component is destroyed, keepFocus will stop running
this._isRunning = false;
}
scanComplete($event) {
var scan = <TextField>this.scannerInput.nativeElement;
// Value of the barcode is evaluated here
scan.text = "";
}
keepFocus() {
setTimeout(() => {
// the input will receive focus every half second
var scan = <TextField>this.scannerInput.nativeElement;
scan.focus();
if(this._isRunning)
this.keepFocus();
}, 500)
}
我的 HTML 是
<StackLayout class="layout-container">
<TextField #scannerInput
name="scannerInput"
id="scannerInput"
(returnPress)="scanComplete($event)">
</TextField>
</StackLayout>
我四处寻找答案,但似乎没有一个适合我的用例。我找到了一些解决方案,可以为整个应用程序/活动禁用软键盘,但是我需要在应用程序的其他部分使用键盘。
【问题讨论】:
标签: nativescript angular2-nativescript