私はMobile App開発用のJavaScriptを使用してJavaScriptで作業しています。私は、bn int countを使ってforループで問題が発生しています。ボタン[bn] .setTitle( '*')は、イベントハンドラで作成されたすべてのボタンで常に5になります。または、forループの外にある// bn = 0のコメントを外すと、値ゼロ。JavaScript Titaniumアプリケーションの割り当て
私の考えでは、作成時には各ボタン、イベントハンドラなどに値を割り当て、カウントを前進させるときに戻ったり変更したりしないでください。私はここで何が欠けているのか、あるいは違うことをする必要がありますか
/**
* create a view object to hold the buttons
* and add the buttons into the view
*/
function createRatingButtons(numButtons,BarTitle,topspace) {
// set vars
var bn=0;
var left = 5;
var top = 5;
/*
* create a view for the buttons
*/
var ratingView = Titanium.UI.createView({
height: 100,
color: 'white',
top: topspace,
});
/*
* create a label to put into the view
*/
var ratingLabel = Titanium.UI.createLabel({
text: BarTitle,
color: '#fff',
backgroundColor: 'transparent',
textAlign: 'left',
height: 'auto',
width: 'auto',
top: 0,
left: left,
})
ratingView.add(ratingLabel);
/*
* do the for loop and add
* the buttons to the view
*/
var button = [];
for(bn==0;bn<numButtons;bn++) {
button[bn] = Titanium.UI.createButton({
title: bn,
width: 50,
height: 50,
color: "black",
// backgroundColor: "blue",
left: left,
top: top+ratingLabel.getHeight(),
});
/*
* Add event handler for this button
*/
button[bn].addEventListener('click', function(e)
{
Ti.API.info("Rating Button Click #: " + bn);
/*
* Update buttons below this count for this object
* to have colored stars, and all starts after this
* to be uncolored.
*/
button[bn].setTitle('*')
});
ratingView.add(button[bn]);
left = left + 50 + 5;
}
//bn = 0;
// return the entire block for this view
return ratingView;
}
'scrollView.add(createRatingButtons(5、" test1 "、0)); scrollView.add(createRatingButtons(5、 "test2"、95)); ' – user1221705