简体   繁体   中英

How do I add an event for a button via code in NativeScript?

I started learning Native Script a few weeks ago, and honestly, I don't like creating an interface through XML, so I want to create it directly through code. Everything was going well until I got to the events.

I have a button:

const btn = new Button();

How do I add an event to it that triggers when I click on it? I know how to do it in XML format, but I want to add an event just like that.

You can do that by adding an event listener - see https://v6.docs.nativescript.org/core-concepts/events

I think you'll find after continuing down this path for a while that defining the ui in xml is actually much easier, but your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM