1

嗨,我正在为选择器制作一个 eventListener。这是我的代码:

var YahooScreen = require("YahooScreen");
var ESPNScreen = require("ESPNScreen");
var CBSScreen = require("CBSScreen");
var ApiWin = Titanium.UI.createWindow({
   backgroundColor : '#fff'
});
var ApiLabel = Titanium.UI.createLabel({
   text  : 'I am Fantasy Expert at',
   top   : '28%',
   left  : '10%',
   right : '10%',
   height: 'auto',
   width : 'auto',
   color : '#000',
   font  : { fontSize : 28,fontWeight : 'bold'},
   textAlign : 'center'
});

ApiWin.add(ApiLabel);

var ApiPicker = Ti.UI.createPicker({
   Top : '35%',
   left : '25%',
   width : '50%'
});

ApiPicker.addEventListener('change', function(e) {
   if (e.row == 'Yahoo!') {
      alert('I am in Yahoo')
   } else if (e.row == 'ESPN') {
      alert('I am in ESPN');
   } else {
      alert('I am in CBS')
   }
});

var data = [];
data.push(Titanium.UI.createPickerRow({
   title : 'Yahoo!'
}));

data.push(Titanium.UI.createPickerRow({
    title : 'Espn!'
}));
data.push(Titanium.UI.createPickerRow({
   title : 'CBS Sports!'
}));

ApiPicker.add(data);
ApiWin.add(ApiPicker);
ApiWin.open();

When Yahoo is selected Yahoo alert should be shown and likewise but it only showing I am in CBS alert could you solve this problem?

谢谢

4

1 回答 1

2

Wahab,这真的很简单。如果你想得到选中的行标题试试,我用下面的代码得到了标题

//Here you will get the title of selected row title
ApiPicker.addEventListener('change', function(e) {
   alert('Selected row = ' + ApiPicker.getSelectedRow(0).title);
   //e.row.title will also give the same effect but above one is the best way
});

读取getSelectedRow 函数

这个也可以

ApiPicker.addEventListener('change', function(e) {
      //alert(e.row.title);
      if('Yahoo!' === e.row.title){
        alert('yahoo');
      } else if('Espn!' === e.row.title){
        alert('espn');
      } else{
        alert('cbs sports');
      }
});

您也可以将数据插入选择器,如下所示

//Your data list
var listData = ["Yahoo!","Espn!","CBS Sports"];
var data = [];
for( var index = 0,len = listData.length; index < len; index++ )
{
    data.push(Titanium.UI.createPickerRow({
        title : listData[index]
    }));
}

ApiPicker.add(data);

这将比您的方法容易得多。如果您遵循此方法,您可以通过将标题添加到数组来创建更多行。否则你的代码会很大。这将是一个简单和容易的。

于 2013-01-07T08:34:17.350 回答