JavaScript Titanium应用程序int赋值

JavaScript Titanium App int assignment

本文关键字:赋值 int 应用程序 Titanium JavaScript      更新时间:2023-09-26

我使用Titanium编写JavaScript,用于移动应用程序开发。我在for循环中遇到了一个关于bn int计数的问题。button[bn].setTitle('*')在事件处理程序中创建的所有按钮上将始终为5,或者如果我取消注释//bn=0;,在for循环之外,它会将所有按钮更新为零值。

在我看来,它应该在创建时为每个按钮、事件处理程序等分配值,而不是在向前移动计数时返回并更改它。我在这里缺少了什么,或者需要做一些不同的事情?

/**
 * 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;
}

试试这个:

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:            20
    });
    ratingView.add(ratingLabel);
    // 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",
            left:   left,
            top:    top+ratingLabel.getHeight()
        });
        // add event handler for buttons
        button[bn].addEventListener('click', function(e) {
            Ti.API.info("Rating Button Click #: " + e.source.title);
            /*
             * Update buttons below this count for this object
             * to have colored stars, and all starts after this
             * to be uncolored.
             */
            e.source.setTitle('*');
        });
        ratingView.add(button[bn]);
        left = left + 50 + 5;
    }
    // return the entire block for this view  
    return ratingView;
}

scrollView.add(createRatingButtons(5, "test1", 0)); 
scrollView.add(createRatingButtons(5, "test2", 95));
  1. 您正试图访问匿名函数中的bn。价值的bn将在执行匿名函数时进行评估。在届时,bn的价值将是numButtons变量。

    因此,请避免在匿名函数中使用bn。你可以使用e.source获取对按钮的引用。

  2. 另外,我认为你错误地使用了

===

在你的循环。

for(bn==0;bn<numButtons;bn++)