0

我正在创建一些 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 并相应地做一些事情。

4

2 回答 2

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.
    }
});
于 2012-12-09T12:36:14.803 回答
1
centerButtons.addEventListener('click', function(evt) {
    alert(evt.source.image);
});
于 2012-12-07T17:28:13.163 回答