JavaScript Titanium App int 赋值

发布于 2025-01-07 13:43:29 字数 2553 浏览 0 评论 0原文

我正在使用 JavaScript 和 Titanium 进行移动应用程序开发。我在 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;
}

I'm working in JavaScript with Titanium for mobile app development. I'm having a problem down in the for loop with the bn int count. button[bn].setTitle('*') will always be 5 on all buttons created in the event handler, or if I uncomment //bn = 0;, which is outside of the for loop, it will update all buttons to be value zero.

In my mind, it should be assigning the value to each button,event handler, etc at the time of creation and not going back and changing it as it moves the count forward. What am I missing here or need to do different?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2025-01-14 13:43:29

试试这个:

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));

Try this:

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));
锦爱 2025-01-14 13:43:29
  1. 您正在尝试访问匿名函数内的 bn。价值
    当匿名函数执行时,将计算 bn 的值。在
    此时 bn 的值将比 的值减 1
    numButtons 变量。

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

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

==

在你的 for 循环中。

for(bn==0;bn<numButtons;bn++)
  1. you are trying to access bn inside the anonymous function. the value
    of bn will be evaluated when the anonymous function gets executes. at
    that time the value of bn will be the 1 less then the value of
    numButtons variable.

    So avoid using bn inside the anonymous function. you can use
    e.source to get the reference to the button.

  2. also, i think you are by mistake using

==

in you for loop.

for(bn==0;bn<numButtons;bn++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文