• Cocos Creator EditBox(编辑框/输入框)添加事件的两种方法


    EditBox添加事件
    方法一
    这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加, 你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对应的 target, component, handler 和 customEventData 参数。

    var editboxEventHandler = new cc.Component.EventHandler();
    editboxEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
    editboxEventHandler.component = "cc.MyComponent"
    editboxEventHandler.handler = "onEditDidBegan";
    editboxEventHandler.customEventData = "foobar";

    editbox.editingDidBegan.push(editboxEventHandler);
    // 你也可以通过类似的方式来注册其它回调函数
    //editbox.editingDidEnded.push(editboxEventHandler);
    //editbox.textChanged.push(editboxEventHandler);
    //editbox.editingReturn.push(editboxEventHandler);

    //here is your component file
    cc.Class({
    name: 'cc.MyComponent'
    extends: cc.Component,

    properties: {   },
    
    onEditDidBegan: function(editbox, customEventData) {
        //这里 editbox 是一个 cc.EditBox 对象
        //这里的 customEventData 参数就等于你之前设置的 "foobar"
    },
    //假设这个回调是给 editingDidEnded 事件的
    onEditDidEnded: function(editbox, customEventData) {
        //这里 editbox 是一个 cc.EditBox 对象
        //这里的 customEventData 参数就等于你之前设置的 "foobar"
    }
    //假设这个回调是给 textChanged 事件的
    onTextChanged: function(text, editbox, customEventData) {
        //这里的 text 表示 修改完后的 EditBox 的文本内容
        //这里 editbox 是一个 cc.EditBox 对象
        //这里的 customEventData 参数就等于你之前设置的 "foobar"
    }
    //假设这个回调是给 editingReturn 事件的
    onEditingReturn: function(editbox,  customEventData) {
        //这里 editbox 是一个 cc.EditBox 对象
        //这里的 customEventData 参数就等于你之前设置的 "foobar"
    }

    });

    方法二
    通过 editbox.node.on('editing-did-began', ...) 的方式来添加
    //假设我们在一个组件的 onLoad 方法里面添加事件处理回调,在 callback 函数中进行事件处理:

    cc.Class({
    extends: cc.Component,
    properties: {
    editbox: cc.EditBox
    },

    onLoad: function () {
       this.editbox.node.on('editbox', this.callback, this);
    },
    
    callback: function (event) {
       //这里的 event 是一个 EventCustom 对象,你可以通过 event.detail 获取 EditBox 组件
       var editbox = event.detail;
       //do whatever you want with the editbox
    }

    });

  • 相关阅读:
    高并发场景 LVS 安装及keepalived的应用
    使用nginx作为http/https正向代理
    Spring5【七】Spring 整合 MyBatis
    Spring5【六】代理模式及 AOP
    MyBatis 配置模板
    Spring5【五】Bean 的自动装配及注解开发
    Spring5【四】依赖注入(DI)
    Spring5【三】IoC 创建对象的方式及配置说明
    Spring5【一】Spring 简介
    MyBatis【七】缓存
  • 原文地址:https://www.cnblogs.com/luorende/p/8113335.html
Copyright © 2020-2023  润新知