Sencha Touch:事件Events

事件是在类发生事件时触发的事件。例如,当单击按钮时或呈现元素之前/之后。

编写事件的方法

以下是编写事件的方法。

  • 使用侦听器的内置事件。
  • 稍后附加事件
  • 自定义事件

使用侦听器的内置事件

Sencha Touch提供了侦听器属性,用于在Sencha Touch文件中编写事件和自定义事件。

用Sencha Touch编写听众

我们将在面板中添加listen属性,从而在之前的程序中添加监听器,如下所示:

<!DOCTYPE html>
<html>
   <head>
      <link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type = "text/javascript">
         Ext.application({
            name: 'Sencha', launch: function() {
               Ext.create('Ext.Panel', {
                  html: 'My Panel', fullscreen: true, listeners: {
                     painted: function() {
                        Ext.Msg.alert('I was painted to the screen');
                     }
                  }
               });
            }
         });
      </script> 
   </head>

这样,我们还可以在listeners属性中编写多个事件。

同一侦听器中有多个事件

现场演示
<!DOCTYPE html>
<html>
   <head>
      <link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type = "text/javascript">   
         Ext.application({
            name: 'Sencha',
            launch: function() {
               var myButton = Ext.Viewport.add({
                  xtype: 'button',
                  centered: true,
                  text: 'Click me'
               });

               myButton.on({
                  tap: function() {
                     var randomWidth = 100 + Math.round(Math.random() * 200);
                     this.setWidth(randomWidth);
                  },
                  widthchange: function(button, newWidth, oldWidth) {
                     alert('My width changed from ' + oldWidth + ' to ' + newWidth);
                  }
               });
            }
         });       
      </script> 
   </head>
</html>

稍后附加活动

在以前的编写事件的方法中,我们在创建元素时已在侦听器中编写了事件。

附加事件的另一种方式如下:

<!DOCTYPE html>
<html>
   <head>
      <link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type = "text/javascript">
         Ext.application({
            name: 'Sencha',
            launch: function() {
               var myButton = Ext.Viewport.add({
                  xtype: 'button',
                  centered: true,
                  text: 'Click me'
               });
               
               myButton.on('tap', function() {
                  alert("Event listener attached by .on");
               });
            }
         });
      </script> 
   </head>
</html>

自定义事件

我们可以在Sencha Touch中编写自定义事件,并使用fireEvent方法触发事件。以下示例说明了如何编写自定义事件。

<!DOCTYPE html>
<html>
   <head>
      <link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type = "text/javascript">
         Ext.application({
            name: 'Sencha',
            launch: function() {
               var myButton = Ext.Viewport.add({
                  xtype: 'button',
                  centered: true,
                  text: "Just wait 5 seconds",

                  listeners: {
                     myEvent: function(button, points) {
                        alert('myEvent was fired! You score ' + points + ' points');
                     }
                  }
               });

               Ext.defer(function() {
                  var number = Math.ceil(Math.random() * 100);
                  myButton.fireEvent('myEvent', myButton, number);
               }, 5000);
            }
         });
      </script> 
   </head>
</html>

页面加载完毕并准备好文档后,将显示带有按钮的UI页面,并且在5秒钟后触发事件时,文档准备就绪后5秒钟后将显示警报框。

在这里,我们编写了自定义事件“ myEvent”,并将事件触发为button.fireEvent(eventName)。

作者:terry,如若转载,请注明出处:https://www.web176.com/senchatouch/1612.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年1月22日 下午5:22
下一篇 2021年1月22日 下午5:33

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注