如何在appcelerator中监听图像视图组上的点击事件

时间:2012-12-07 16:35:19

标签: titanium appcelerator titanium-mobile appcelerator-mobile

我正在创建一些imageViews,如下所示。

// 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并做了相应的事情。

2 个答案:

答案 0 :(得分: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.
    }
});

答案 1 :(得分:1)

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