带有Titanium的TextFields数组

时间:2013-11-20 19:33:45

标签: javascript titanium

我需要在屏幕上放置一个6x10的单元格矩阵。我已经发现我可以创建一个文本字段数组,如何为每个元素的顶部和左侧分配值?

为了清晰起见编辑:我需要将这些单元格分别显示在10行,每行6个单元格中。

var textFields2 = [];
for (var i = 0; i < 59; i++)
 {

    textFields2[i] = Ti.UI.createTextField({
        borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
        width : '30dp',
        height : '45dp',
        //value : '',
        top : '5%',    // starting row 
        color : '#000000',
        left   : '0%'  // starting column 
    });
 }

1 个答案:

答案 0 :(得分:0)

好吧,要做任何事情,你需要将它们添加到容器中。您还需要正确设置宽度和高度,使它们适合屏幕,10行5个文本字段意味着每个文本字段的高度必须为10%,宽度为20%。然后,您可以使用水平布局属性来创建网格。

这是一个主要是自包含的,可能不是无bug的例子:

var main = Ti.UI.createWindow();

var container = Ti.UI.createView({
    layout : 'horizontal',
    horizontalWrap : true, // this is the default
    width : Ti.UI.FILL,
    height: Ti.UI.FILL
});

// Set this and then calculate below
var rows = 10, columns = 5;

// Define the base attributes of a textfield
var baseAttrs = {
    borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
    width : (100 / columns) + '%',
    height : (100 / rows) + '%',
    color : '#000000'
};

// Add textfields to the container with the base attributes
for(var i = 0; i < rows * columns; i++) {
    var tfield = Ti.UI.createTextField(baseAttrs);
    // Set an initial value
    tfield.value = i;
    container.add(tfield);
}

// Add container and open the window
main.add(container);
main.open();