我正在使用带有 NativeScript 的 Telerik AppBuilder 编写跨平台移动应用程序。我快要发疯了,想弄清楚如何在 TextField 上获得基本的“blur”或“onTextChanged”事件。我可以弄清楚如何执行“点击”事件之类的操作,但为什么很难找到执行“模糊”或“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 中没有 blur(-like) 事件。但是,您可以在文本更改时使用react。
您要做的是利用 Data Binding Mechanisms和 Observable Object在 NativeScript 中。
简而言之,数据绑定(bind)将允许您将用户界面(通常在您的 XML 文件中描述)与您的业务模型/数据对象连接起来。数据绑定(bind)可以是“单向”的,这意味着您在数据对象中所做的任何更改都将反射(reflect)在 UI 中。它也可以是两种方式,这意味着您在 UI 中所做的任何更改也将反射(reflect)在您的数据对象中。
在您的情况下,您需要两种方式的绑定(bind),因为您希望 UI(用户输入文本)中发生的任何事情都反射(reflect)在您的模型中。
例子
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;
关于javascript - Telerik Nativescript 移动应用程序中的基本模糊事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485879/