1

我不是贸易程序员,但更像是一个修补匠(菜鸟)

我想从数组中包含的标题创建一个菜单。我通过循环遍历数组并使用循环每一步的值作为我的按钮/选项卡的标题,成功地做到了这一点。

我想为每个按钮添加一个事件监听器,并带有指向唯一页面的链接。我本来打算通过检查按钮的标签来做到这一点,如果它是值 A,那么它会得到一个相应的链接,依此类推,然后将其分配给 currentLink,然后在每次通过时由 theLink[t] 引用环形。

我想我几乎让它工作了,但只是在动态添加链接位时迷失了。

任何帮助将非常感激。

这是我的代码:

//function to check the label name and assign the relevant filename to be passed on
    function winLink(currentTab){
     if (currentTab == 'Audio'){
      currentLink = 'audioPlayer.js';
      pageWin.url = currentLink;

     } else{
      if (currentTab == 'Images'){
      currentLink = 'images.js';
      }
     } 
    }

//function to change button colours according to active button
function button_colour(e){
 if (clickedButton == ''){
  clickedButton = e.source; 
  clickedButton.backgroundImage = tabBckImgSel;

 } else {
  clickedButton.backgroundImage = tabBckImg;
  clickedButton = e.source; 
  clickedButton.backgroundImage = tabBckImgSel;
  }
}


    //Array that contains button names
    var tabsArray =['Audio','Images','Video','Info','Materials','Further Reading','Map'];

//loop through array and create a holder, label and eventlistener to attach
for (var t=0;t<tabsArray.length;t++) {
 //create view to act as button/tab area
 viewButton[t] = Titanium.UI.createView({
  borderWidth:1,
  borderColor: tabBrdrColour,
  width:tabWidth,
  height:tabHeight,
  left:leftTab + padding
 });

 //add the tab to the main menu view/area
 menu.add(viewButton[t]);

 //increment the left position for the next button to be placed to the right of the previous button
 leftTab = viewButton[t].left + tabWidth;

 Buttonlabel[t] = Titanium.UI.createLabel({
  text: tabsArray[t],
  font:{fontSize:13},
  color:'#fff',
  width:labelWidth,
  textAlign:'center',
  height:'auto'
 });

 viewButton[t].add(Buttonlabel[t]);

    //pre declared variable  to hold the title of the current label
 currentTab = tabsArray[t];
//call to function to check label and set the relevant file to load
 winLink(currentTab);

 viewButton[t].addEventListener('singletap', function(e)
  {

  button_colour(e);
  win.add(pageWin);
var theLink[t] = currentLink;
  // alert (' Current link' + currentLink);
 });

}
4

1 回答 1

0

哈希表会是您正在寻找的吗?它将允许您动态创建和使用这些链接。

http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

我也想改变这个...

  //function to check the label name and assign the relevant filename to be passed on
function winLink(currentTab){
 if (currentTab == 'Audio'){
  currentLink = 'audioPlayer.js';
  pageWin.url = currentLink;
  } else{
  if (currentTab == 'Images'){
  currentLink = 'images.js';
  }
 } 
}

到...

       //function to check the label name and assign the relevant filename to be passed on
function winLink(currentTab){
 if (currentTab == 'Audio'){
  currentLink = 'audioPlayer.js';
  pageWin.url = currentLink;
  } else if(currentTab == 'Images'){
  currentLink = 'images.js';
  }
 } 

这让我很烦:)

于 2010-12-03T13:35:40.113 回答