2009-12-17 61 views
0

ActionScript 3的ActionScript 3的分佈的TextField

我已經衝倒我的頭今天最上要對此最好的辦法,但我最終在複雜的什麼,我會認爲是一個簡單的任務。

我有一些的TextField一字排開彼此相鄰的所有固定寬度,即([寬])

 tf1   tf2  tf3  tf4  tf5 
[  80  ][ 50 ][ 50 ][ 50 ][ 70 ] 

我想要做的是能夠選擇我要顯示其細節,然後將刪除的TextField的寬度重新分配給其餘部分(注意只能從右側刪除),所以如果我要顯示「tf3」,則需要將120(70 + 50)均勻分配到其他框:

[  80 + 50 + 50 = 180     ] 
      tf1 
[80/180*120 + 80 = 133.3] 
         tf2 
       [50/180*120 + 50 = 83.3] 
            tf3 
         [50/180*120 + 50 = 83.3] 

然後排隊起來再次(X +寬度等):

[  133.3  ][  83.3  ][  83.3  ] 

任何想法的更好的方法,我肯定有一些不錯的簡單功能,我可以用來做到這一點。

我可能已經過於複雜的問題現在也,但生病給它一個嘗試,任何人有什麼想法?

回答

1

因此,讓你有不同寬度N個文本字段我得到這個直,你想刪除1或者更多,重新排列剩餘的M個文本字段,按比例重新分配它們的寬度(即,如果文本字段是文本字段T的寬度的兩倍,那麼在它們重新排列後它仍然會是)。

這實際上不是一個非常簡單的問題,但我在下面進行了刺探。您將文本字段數組(從左到右),要保留的數字以及可選的X偏移量傳遞給它。

注意:我還沒有在幾個月內完成AS3所以考慮下面是僞代碼 - 你將不得不編輯它才能工作。

function arrangeTextFields(textFields:Array, keep:int, xOffset:Number = 0) { 
    // we can't keep more fields than are provided 
    keep = Math.min(keep, textFields.length); 

    var totalWidth = 0, // the total width of all textfields 
     keptOrigWidth = 0, // the total combined widths of the ones you're keeping 
     origNum:int = textFields.length; 

    // calculate the above values by addition in a loop 
    // (because AS3 lacks an inject/reduce method) 
    for(var i:int, i++, i < origNum) { 
    totalWidth += textFields[i].width; 

    if(i < take) { 
     keptOrigWidth += textFields[i].width; 
    } 
    } 

    var ratio:Number = totalWidth/takenOrigWidth, // the ratio we'll resize each field by to keep them proportional 
     currentX:int = 0; 

    textFields = textFields.slice(0, keep - 1); // throw away the fields we're not keeping 

    for(int i = 0; i < take; i++) { 
    textFields[i].width = textFields[i].width * ratio; // resize it 
    textFields[i].x  = currentX + xOffset;   // move it 

    currentX += textFields[i].width; 
    } 
} 

注:此代碼實際上並沒有從視圖中刪除了「unkept」文本字段;你必須添加它或在別處做。

1

上午我錯理解的東西,或者是不是很簡單:

boxWidth = (availableWidth - (padding + margins))/numberOfFieldsShown