简体   繁体   English

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

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

I want to be able to put a different button inside every row (createTableViewRow). 我希望能够在每行(createTableViewRow)中放置一个不同的按钮。 I have created five buttons (Titanium.UI.createButton), but I don't know how to place all five of my buttons for each created row. 我已经创建了五个按钮(Titanium.UI.createButton),但是我不知道如何为创建的每一行放置所有五个按钮。 Can somebody give me a hint on how to do it? 有人可以给我提示如何做吗?

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

You should add the listener event on the tableView, not on each buttons inside the row : 您应该在tableView上而不是在行内的每个按钮上添加侦听器事件:

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

Try this, 尝试这个,

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

This answer find from here 这个答案从这里找到

UPDATE : 更新:

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

Don't forget to mark this as correct answer If this useful. 如果有用,请不要忘记将其标记为正确答案。

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

相关问题 我如何在每一行上添加删除按钮? - How Can i add Delete button on each row pls? 我如何 append 删除按钮列到每个表行 - How can I append delete button column to each table row 如何为表格中的每一行制作一个接受按钮? - How can I make an accept button for each row in the table? 我在表的每一行中都有一个按钮:OnClick处理程序如何分辨从哪一行调用它? - I have a button in each row of a table: How can the OnClick handler tell which row it was called from? 如何在 VueJS 中动态添加输入,其中每个下拉列表都指定给同一行中的单选按钮组? - How can I dynamically add inputs in VueJS wherein each dropdown is designated to a radio button group in the same row? 如何为表行结果的每个按钮添加事件侦听器? - How can I add an event listener to each button for a table row result? 如何识别特定项目 <select>并在jquery中的每一行添加一个删除按钮 - How can I identify a specific item in <select> and add a delete button each row in jquery 如何根据单击按钮将每行总和添加两个? - How can I add two each row sum depending on a button click? 如何使用Titanium Studio在Android上创建返回上一页的后退按钮? - How to create a back button to previous page on Android with Titanium Studio? 在幻灯片上,如何在幻灯片的每一侧放置箭头? - On the slideshow how can i place the arrows on each side of the slideshow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM