【问题标题】:How can I place a button on each row in titanium studio?如何在钛工作室的每一行上放置一个按钮?
【发布时间】:2014-03-25 12:10:20
【问题描述】:

我希望能够在每一行中放置一个不同的按钮(createTableViewRow)。我创建了五个按钮 (Titanium.UI.createButton),但我不知道如何为每个创建的行放置所有五个按钮。 有人可以给我一个提示吗?

function sendAjax() {

    var xhr = Titanium.Network.createHTTPClient();

    xhr.onerror = function(e){
        var error = e.error;
        alert(error);               
    };
    xhr.open('GET', 'http://xxxxxxxxxxxxxx');
    xhr.send();

    var tv = Titanium.UI.createTableView({
        height: Titanium.UI.SIZE,
        width: Titanium.UI.FILL
    });
    win2.add(tv);

    xhr.onload = function() {

        var data = [];
        var schools = JSON.parse(this.responseText);

        for (s in schools) {
            data.push(Titanium.UI.createTableViewRow({
            title: schools[s].Name
        }));
    }
    tv.data = data;
    };
}

【问题讨论】:

    标签: javascript android titanium httpclient


    【解决方案1】:

    您应该在 tableView 上添加侦听器事件,而不是在行内的每个按钮上:

    tableView.addEventListener("click", function (event) {
        if (event.source.buttonid) {
           //it's a button
        }
    });
    

    【讨论】:

    • 嗨,不错的代码,它解决了我的问题,即每个按钮都有不同的侦听器。
    【解决方案2】:

    试试这个,

    var win = Ti.UI.createWindow({
    backgroundColor : '#fff'
    });
    
    var tableData = [];
    
    for(var i = 0; i < 10; i++) {
    var row = Ti.UI.createTableViewRow();
    var button = Ti.UI.createButton({
        title : 'Button ' + i,
        width : 100,
        height : 40,
        buttonid : i    //our custom button property
    });
    row.add(button);
    tableData.push(row);
    
    button.addEventListener('click', function(e) {
        Ti.API.info('button ' + e.source.buttonid + ' clicked.');
        alert('Button ' + e.source.buttonid);
    });
    }
    
    var tableView = Ti.UI.createTableView({
    data : tableData
    });
    
    win.add(tableView);
    
    win.open();
    

    这个答案来自here

    更新:

    function sendAjax() {
    
    var xhr = Titanium.Network.createHTTPClient();
    
    xhr.onerror = function(e){
        var error = e.error;
        alert(error);               
    };
    xhr.open('GET', 'http://xxxxxxxxxxxxxx');
    xhr.send();
    
    var tv = Titanium.UI.createTableView({
        height: Titanium.UI.SIZE,
        width: Titanium.UI.FILL
    });
    win2.add(tv);
    
    xhr.onload = function() {
    
        var data = [];
        var schools = JSON.parse(this.responseText);
        for (s in schools) {
        var row = Ti.UI.createTableViewRow();
        var rowItem = Ti.UI.createView({
           height : 40,
           width : Ti.UI.FILL,
           layout : 'horizontal'
        });
        row.add(rowItem);
        var title = Ti.UI.createLabel({
           height : 40,
           text : schools[s].Name,
           width : Ti.UI.SIZE
        });
    
        rowItem.add(title);
        var button = Ti.UI.createButton({
            title : 'click ',
            width : 100,
            height : 40,
            buttonid : s    //our custom button property
        });
        rowItem.add(button);
        data.push(row);
    
        button.addEventListener('click', function(e) {
            Ti.API.info('button ' + JSON.stringify(e.source.buttonid) + ' clicked.');
    
        });
        };
    
        tv.data = data;
    };
    };
    

    如果有用,别忘了标记为正确答案。

    【讨论】:

    • 您好,代码不错,但是您在代码中的哪个位置使用了 json GET?我可以为按钮签署不同的adventListener 吗?
    • 你能告诉它哪一行显示错误。请确保您在 for 循环之外声明了 var data = []。
    • 你得到答案了吗?对我来说,它对我来说很好。
    • 不错的代码,但是当我单击按钮时没有任何反应... 日志显示:单击了 [INFO] 按钮对象。我想从 json 文件中填充我的行...
    • 更改按钮点击监听 Ti.API.info('button' + JSON.stringify(e.source.buttonid) + 'clicked.');实际上,您将行 obj 作为 json。要打印 json,您需要使用 JSON.stringify(jsonObj)
    猜你喜欢
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多