您好我正在为picker制作一个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();
当雅虎被选中时,应该显示雅虎警报,但同样只显示我在CBS警报中你能解决这个问题吗?
由于
答案 0 :(得分:2)
//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
});
这个也会起作用
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);
这比你的方法容易得多。如果您遵循此方法,则可以通过将标题添加到数组来创建更多行。否则您的代码大小会很大。这将简单易行。