我的安卓应用的第一页有一个输入框。我想要的是,当页面加载时,自动显示键盘,而不需要在输入框上点击。
pkanev的评论在iOS上是正确的,即只需聚焦于文本字段,iOS就会打开键盘。
但在Android上,您需要做一些额外的工作--
var utils = require("tns-core-modules/utils/utils");
var myTextfield = page.getViewById("myTextFieldId");
if (myTextfield.ios) {
console.log("myTextfield.ios");
// on ios this will open the keyboard but not on android
myTextfield.focus();
}
if (myTextfield.android) {
console.log("myTextfield.android");
setTimeout(function() {
// places the cursor here but doesn't open the keyboard
myTextfield.android.requestFocus();
var imm = utils.ad.getInputMethodManager();
imm.showSoftInput(myTextfield.android, 0);
}, 300);
}
Name your element in your template: <TextField #myInput [...]></TextField>
Use it in your component file:
import { ElementRef, OnInit } from "@angular/core";
[...]
export class MyComponent implements OnInit {
@ViewChild("myInput") myInput: ElementRef;
[...]
ngOnInit() {
setTimeout(() => this.myInput.nativeElement.focus(), 0);
}
}