Titanium,为屏幕上按下的任何按钮创建动作监听器

时间:2013-06-25 16:52:51

标签: javascript function event-handling titanium

所以我在屏幕上有9个按钮,我想要一个动作监听器:

-sudo code-

    frame3.buttion.event-handle({ this.title='clicked'})

-sudo code-

我现在的方式是拥有9个不同的按钮和事件处理程序大声笑,而不是很酷。还有一种方法可以创建一个按钮数组,并从该数组中添加和操作它们,如array [0] .title = clicked?

2 个答案:

答案 0 :(得分:1)

是的,

var buttons = new Array();
buttons[i] = Ti.UI.createButton({
    ..........
    //Add this
    my_id:i
});

稍后可以再次提取

buttons[i].addEventListener('click',function(e)){ 
    var i = e.source.my_id;
    myAction[i] = Ti.Media.createSound({ url: sounds[i] }).play();
    Ti.API.info("clicked button: " + i+ " : "+ myAction[i]);
});

buttons[i].addEventListener('click',function(e)){ 
    var i = e.source.my_id;
    doSomething(i); //function that handles click.
});

答案 1 :(得分:1)

我会推荐一种完全不同的方法。

1) create a view and place all of the buttons in the view.
2) associated one eventListener to the view containing the buttons
3) when the view gets a click event, it will bubble up to the buttons; check the
  event.source.id to determine which button was clicked.