繁体   English   中英

如何在Titanium Studio的每一行上放置一个按钮?

[英]How can I place a button on each row in titanium studio?

我希望能够在每行(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;
    };
}

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

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

尝试这个,

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

这个答案从这里找到

更新:

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

如果有用,请不要忘记将其标记为正确答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM