有没有办法将 onFocus 事件绑定到 NativeScript 中的 TextField?

Is there a way to bind an onFocus Event to a TextField in NativeScript?

显然我无法在 NativeScript 的 documentation or API reference TextFields 元素中找到焦点事件处理程序。我错了吗?

或者,有没有办法引入扩展现有 Nativescript 元素的 onFocus 事件?

(我使用的是没有 Angular 2 的打字稿)

您可以使用 here 提到的 on 方法。我不知道这是否是您要查找的内容,但这是一个示例:

在XML中:

<TextView id="textfield" textWrap="true" tap="textfieldTap"/>

在js文件中:

var myTextfield = page.getViewById("textfield");

myTextfield.on("tap", function() {
    console.log("Hello from the other side");
});

此外,还有另一种方法可以监听 TextField 上的焦点事件。由于我们在 XML TextField 标记中有 tap="textfieldTap",因此我们可以在 js 中声明一个函数来监听该事件:

function textfieldTap(args) {
    console.log("Textfield tap " + args.text);
}
exports.textfieldTap = textfieldTap;

A PR for adding focus 正在等待审核。不久之后您就可以在 TextFieldTextView 元素上使用它了!