我正在使用Telerik AppBuilder和NativeScript编写一个跨平台的移动应用程序。我要疯了,试图找出如何得到一个基本的"模糊"或"onTextChanged"事件上的TextField。我可以弄清楚如何做"点击"事件之类的事情,但为什么很难找到一个人做"模糊"或"onTextChanged"的例子?
我试过了:
var tagField = view.getViewById(page, "tfDescription")
tagField.set("onblur", function(eventData) {
pageData.set("debug", "blur this field: " + JSON.stringify(eventData));
});
我也尝试了所有我能想到的属性在xml:
<TextField id="tfDescription" blur="blur" onblur="blur" onLoseFocus="blur" focusLost="blur" textChanged="blur" row="2" text="{{ description }}" />
这些都不起作用。有人知道怎么做吗?
谢谢
据我所知,在NativeScript中没有模糊(类似)事件。但是,当文本被更改时,您可以做出反应。
你要做的是利用数据绑定机制和原生脚本中的可观察对象。
简单地说,数据绑定允许您将用户界面(通常在XML文件中描述)与业务模型/数据对象连接起来。数据绑定可以是"单向的",这意味着您对数据对象所做的任何更改都将反映在UI中。它也可以是两种方式,这意味着您在UI中所做的任何更改也将反映回您的数据对象。
在您的情况下,您需要两种方式绑定,因为您希望UI(用户输入文本)中发生的任何事情都反映在您的模型中。
例子 xml>假设你有这样一个xml:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<StackLayout>
<TextField text="{{ description }}" />
</StackLayout>
</Page>
js
为了更容易理解,我已经注释了内联。
var observable = require("data/observable");
function pageLoaded(args) {
var page = args.object;
/**
* Creating a new Observable object.
*/
var contextObj = new observable.Observable();
/**
* Setting the property 'description' to 'hi there'.
* In your XML you'll attach this property to the
* Text field as such:
* <TextField text="{{ description }}" />
* This means that the Text field will, by default
* be prepopulated with 'Hi there'
*/
contextObj.set("description", "Hi there");
/**
* Attaching the observable object to the
* 'bindingContext' of the page. This is a
* special property to which you want to attach
* the object. By default bindings are two way.
*/
page.bindingContext = contextObj;
/**
* Event listener.
* When the user is changing the text in the UI, this gets propagated down
* to the contextObj which will change. Here we add an event listener for
* changes and log some information about the change that just happened.
*/
contextObj.addEventListener(observable.Observable.propertyChangeEvent, function (event) {
console.log('The object changed!');
console.log('Event: ' + event.eventName);
console.log('propertyName: ' + event.propertyName);
console.log('New value: ' + event.value);
console.log('');
});
}
exports.pageLoaded = pageLoaded;
在NativeScript 3中添加了blur
事件,可以与TextView
或TextField
组件一起使用。
编辑现在也有一个focus
事件即将从这个拉请求合并。
好吧,我没有把这标记为正确的解决方案,但如果有人看到这将是有帮助的。在Telerik中,NativeScript似乎有一些细微的差异。你需要学会阅读库文件,比如tns_modules/data/observable/observable.js,以便自己弄清楚这些东西。例如,要创建属性更改侦听器,您需要使用以下语法:
pageData.on(observableModule.knownEvents.propertyChange, function(propertyChangeData){
if (propertyChangeData.propertyName != "debug"){
pageData.set("debug", propertyChangeData.propertyName + " has been changed and the new value is: " + propertyChangeData.value);
}
});
注意你使用了"observableModule.knownEvents. "将"observable.Observable.propertyChangeEvent"替换为" propertyChange"。您不必像我所做的那样使用函数"on"代替"addEventListener"。函数"on"实际上只是转过来调用"addEventListener"。
我希望这有助于那里的人。
在nativescript 3+中,我是这样捕获模糊事件的。
示例代码在native - script (core)
example.js
const view = require("ui/core/view");
const textFieldModule = require("ui/text-field");
exports.pageLoaded = (args) => {
let page = args.object;
let myTextFieldView = view.getViewById(page, "myTextField");
myTextFieldView.on(textFieldModule.TextField.blurEvent, function(eventData) {
console.log('blur event triggered');
}, this);
}
example.xml
<Page loaded="pageLoaded">
...
<TextField
id="myTextField"
hint="My Text Field"
text="{{ myTextField }}" />
...
</Page>