0

我正在创建一些imageViews,如下所示。如何在appcelerator中监听图像组中的单击事件

// Time buttons 
    var timeButtons =[ 
     { title: 'Afternoon', path: 'images/others/time/afternoon.png'}, 
     { title: 'CurrentTime', path: 'images/others/time/currenttime.png'}, 
     { title: 'Evening', path: 'images/others/time/evening.png'} 
    ] 


createButtons(timeButtons); 


/* Button creation function */ 
function createButtons(data){ 

    for (var i = 0; i < data.length; i++){ 
     //Creating each button 
     var button = Titanium.UI.createImageView({ 
      image: data[i].path, 
      height: 90, 
      width: 90, 
      top: 20+90*i+20*i, 
      value: 1 
     }); 

     //Adding the buttons to the center view 
     centerButtons.add(button); 
    } 
} 

现在,当用户点击任何一个ImageView的,我要确定哪些ImageView的点击它,并做相应的事情。

回答

1
centerButtons.addEventListener('click', function(evt) { 
    alert(evt.source.image); 
}); 
2

你可以额外参数添加到您的ImageView。像ID左右。 这里有一个额外的参数,你的例子:

for (var i = 0; i < data.length; i++){ 
    //Creating each button 
    var button = Titanium.UI.createImageView({ 
     _id: i, // Your custom parameter 
     image: data[i].path, 
     height: 90, 
     width: 90, 
     top: 20+90*i+20*i, 
     value: 1 
    }); 

    //Adding the buttons to the center view 
    centerButtons.add(button); 
} 

比你可以使用新的参数,以确定您的ImageView:

centerButtons.addEventListener('click', function(event) 
{ 
    // Perhaps you should check here if the custom parameter (_id) exist. 

    switch(event.source._id){ 
     case 0: 
      // Your stuff with you first image 
      break; 
     // etc. 
    } 
}); 
相关问题