2013-11-26 34 views
0

此代码实现60单元电子表格,6x10行,列。在每行的末尾有两个标签,一个用于总行数和总数。这里的主要问题是如何在按下calc按钮时更新行和运行总标签。更新单元矩阵中的数据

我也想将按钮从scrollview的底部移动到窗口的底部,它总是可见的。底部按钮的另一种观点?

Ti.include("toast.js"); // 3rd-party code; displays notifications 

var i=0; 
var r=0; 
var rows = 10; 
var columns = 6; 

left = ["0%","12%","24%","36%","48%","60%"]; 



Titanium.UI.setBackgroundColor('#000'); 

var win1 = Titanium.UI.createWindow({ 
    title:'Target 1', 
    exitOnClose: true, 
    backgroundColor:'#fff' 
}); 


win1.addEventListener('androidback' , function(e){ 
    win1.close(); 
    var activity = Titanium.Android.currentActivity; 
    activity.finish(); 
}); 

var scrollView1 = Ti.UI.createScrollView({ 
    bottom:120, 
    contentHeight: 'auto', 
    layout: 'vertical' 
}); 


if (Ti.UI.Android){ 
    win1.windowSoftInputMode = Ti.UI.Android.SOFT_INPUT_ADJUST_PAN; 
} 


var buttonCalc = Titanium.UI.createButton({ 
    title: 'Calc', 
    top: 10, 
    width: 100, 
    height: 50, 
    left: "10%" 
}); 




var lbAttrs1 = { 
      text: "000", 
      left: "74%", 
      color:'#000',width:'auto',height:'auto',textAlign:'left', 
      font:{fontSize:24,fontWeight:'regular'} 
      }; 


var lbAttrs2 = { 
      text: "000", 
      left: "88%", 
      color:'#000',width:'auto',height:'auto',textAlign:'left', 
      font:{fontSize:24,fontWeight:'regular'} 
      }; 


var baseAttrs = { 
    borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED, 
    keyboardType: Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION, 
    maxLength: 2, 
    top: 10, 
    height: 60, 
    value: "", 
    width: '12%', 
    color : '#000000' 
}; 

var tfields = []; 
var labels1 = []; 
var labels2 = []; 


buttonCalc.addEventListener('click',function(e) 
{ 

    var a = 0; 
    var b = 0; 

for (j=0;j<rows;j++) 
{ 
    a = 0; 

    for (i=0;i<columns;i++) 
    a = parseInt(tfields[j][i].value) + a; 

    b = b + a; 

    labels1[j] = a.toString(); 
    labels2[j] = b.toString(); 
} 

for (j=0;j<rows;j++) 

alert(labels1[j]+' '+labels2[j]+ ' ' + j.toString()); 

}); 


function createRow1(i) // start create row 

{ 

    row1 = Ti.UI.createView({ 
    backgroundColor: 'white', 
    borderColor: '#bbb', 
    borderWidth: 1, 
    width:'100%', height: 70, 
    top: 0, left: 0 }); 

var tfield1 = []; 
var label1 = []; 
var label2 = []; 

for (i=0;i<columns;i++) 
    { 
    tfield1[i] = Ti.UI.createTextField(baseAttrs); 
    label1[i] = Ti.UI.createLabel(lbAttrs1); 
    label2[i] = Ti.UI.createLabel(lbAttrs2); 
    } 


    tfield1[0].addEventListener('change', function() 
    { 
    if (tfield1[0].value > 10) 
     { 
     tfield1[0].value = ""; 
     showMessageTimeout("More than 10.",15); 
     } 
    }); 

tfield1[1].addEventListener('change', function() 
    { 
    if (tfield1[1].value > 10) 
     { 
     tfield1[1].value = ""; 
     showMessageTimeout("More than 10.",15); 
     } 

    }); 


    tfield1[2].addEventListener('change', function() 
    { 
    if (tfield1[2].value > 10) 
     { 
     tfield1[2].value = ""; 
     showMessageTimeout("More than 10.",15); 
     } 
    }); 



    tfield1[3].addEventListener('change', function() 
    { 
    if (tfield1[3].value > 10) 
     { 
     tfield1[3].value = ""; 
     showMessageTimeout("More than 10.",15); 
     } 

    }); 

    tfield1[4].addEventListener('change', function() 
    { 
    if (tfield1[4].value > 10) 
     { 
     tfield1[4].value = ""; 
     showMessageTimeout("More than 10.",15); 
     }  
    }); 

tfield1[5].addEventListener('change', function() 
    { 
    if (tfield1[5].value > 10) 
     { 
     tfield1[5].value = ""; 
     showMessageTimeout("More than 10.",15); 
     }   
    }); 


tfield1[0].left = left[0]; 
tfield1[1].left = left[1]; 
tfield1[2].left = left[2]; 


tfield1[3].left = left[3]; 
tfield1[4].left = left[4]; 
tfield1[5].left = left[5]; 


for (i=0;i<columns;i++) 
{ 
    row1.add(tfield1[i]); 
    row1.add(label1[i]); 
    row1.add(label2[i]); 
    } 

; 
tfields.push(tfield1); 
labels1.push(label1); 
labels2.push(label2); 

return row1; 
} /// end of createrow1 

for(i = 0; i < rows; i++){ 
row1 = createRow1(i); 
scrollView1.add(row1); 
} 

win1.add(scrollView1); 
scrollView1.add(buttonCalc); 
// win1.add(buttonCalc); 

win1.open(); 

回答

0

点1

这是一个笔误,你应该的labels1[j]labels2[j]每次出现替换到labels1[j].textlabels2[j].text分别为:

buttonCalc.addEventListener('click',function(e) 
{ 

    var a = 0; 
    var b = 0; 

    for (j=0;j<rows;j++) 
    { 
     a = 0; 

     for (i=0;i<columns;i++){ 
      var newValue = parseInt(tfields[j][i].value); 
      if(!isNaN(newValue) && typeof(newValue) === 'number') 
       a = newValue + a; 
     } 

     b = b + a; 

     labels1[j].text = a.toString(); 
     labels2[j].text = b.toString(); 
    } 
    for (j=0;j<rows;j++) 
     Ti.API.info(labels1[j].text +' '+labels2[j].text + ' ' + j.toString()); 

}); 

而且还必须改变这些部分:

function createRow1(i) // start create row 
{ 
... 
    for (i=0;i<columns;i++) 
    { 
     tfield1[i] = Ti.UI.createTextField(baseAttrs); 
    } 
    label1 = Ti.UI.createLabel(lbAttrs1); //there is only one per row 
    label2 = Ti.UI.createLabel(lbAttrs2); //there is only one per row 
... 
    for (i=0;i<columns;i++) 
    { 
     row1.add(tfield1[i]); 
    } 
    row1.add(label1); //there is only one per row 
    row1.add(label2); //there is only one per row 

    tfields.push(tfield1); 
    labels1.push(label1); 
    labels2.push(label2); 

    return row1; 
} /// end of createrow1 

点2

你能做到这样:

var scrollView1 = Ti.UI.createScrollView({ 
    top:0, 
    height:'60%', //Put your desired value here 
    contentHeight: 'auto', 
    layout: 'vertical' 
}); 

... 

var buttonCalc = Titanium.UI.createButton({ 
    title: 'Calc', 
    top: '70%', // Put your desired value here greater than scrollView1.height 
    width: 100, 
    height: 50, 
    left: "10%" 
}); 

... 
win1.add(scrollView1); 
//scrollView1.add(buttonCalc); 
win1.add(buttonCalc); 

加分。

你需要设置softKeyboardOnFocus财产在你baseAttrs:

var baseAttrs = { 
     borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED, 
     keyboardType: Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION, 
     softKeyboardOnFocus: Titanium.UI.Android.SOFT_KEYBOARD_SHOW_ON_FOCUS, 
     maxLength: 2, 
     top: 10, 
     height: 60, 
     value: "", 
     width: '12%', 
     color : '#000000' 
    }; 

这样,softKeyboard将焦点在第一时间显示。

最后,祝你好运:-)

+0

感谢您的意见。我仍然需要更新每行末尾的总字段(label1 []和label2 [])。做label1 [j] .text = labels1 [j] .text;在calc函数失败,因为label1和label2被视为未定义,范围错误? – Jocala

+0

好的。我看到了,我编辑了第1点,现在它运行良好,我在我的手机上进行了测试:-)谢谢 – kabomi

+0

在这里很好用。非常感谢! – Jocala