我正面临这个问题,我正在从一个项目数组(字符串)生成动态滑块,我使用一个对象来动态创建变量,然后将一个事件监听器添加到我刚创建的变量中但是它不行。
编辑:
变量itemsObject[labelValue]
在检测到更改事件时,它不会更改其文本值。
var items = ['Love',
'Angry',
'Jealous',
'Happiness',
'Understanding'];
//all the controls on this view
var itemsObject = {};
var scrollView = Titanium.UI.createScrollView({
contentWidth:'auto',
contentHeight:'auto',
top:0,
showVerticalScrollIndicator:true,
showHorizontalScrollIndicator:false,
touchEnabled: true
});
var viewInsideScrollView = Ti.UI.createView({
width:'310dp',
height:'auto',
top:0,
touchEnabled:false
});
scrollView.add(viewInsideScrollView);
var fromTopItem =30,
fromTopSlider=50,
fromTopLabelValue =25;
for(i in items){
var label = items[i] + 'Label';
//item name
itemsObject[label] = new Ti.UI.createLabel({
text: items[i],
color: '#ffffff',
top:fromTopItem,
height:'auto',
width:'auto'
});
fromTopItem+=70;
var slider = items[i] + 'Slider';
//slider control
itemsObject[slider] = Titanium.UI.createSlider({
top:fromTopSlider,
min: 0,
max: 5,
minRange: 0,
maxRange:5,
width: '95%',
value: parseInt(Math.random() * (5 - 0) + 0) //values from 0 to 5
});
fromTopSlider+=70;
var labelValue = items[i] + 'LabelValue';
//value selected
itemsObject[labelValue] = new Ti.UI.createLabel({
text: itemsObject[slider].value,
color: '#ffffff',
top:fromTopLabelValue,
left:285,
height:'auto',
width:'auto',
font:{
fontSize:24,
fontWeight: 'bold'
}
});
fromTopLabelValue+= 70;
//listener
itemsObject[slider].addEventListener('change', function(e){
itemsObject[labelValue].text = parseInt(e.value);
});
//adding to the view
viewInsideScrollView.add(itemsObject[label]);
viewInsideScrollView.add(itemsObject[labelValue] );
viewInsideScrollView.add(itemsObject[slider] );
}
self.add(scrollView);
答案 0 :(得分:0)
我解决了它,这是范围问题,所以我将控件创建放在一个函数中,然后用循环调用该函数。