【问题标题】:How to access the array elements如何访问数组元素
【发布时间】:2010-10-27 05:45:10
【问题描述】:
var count:uint = 0;
var textInputs:Array /* of TextInputs */ = [];
for(var i:String in columnsData){
    textInputs[count] = new TextInput();

    addChild(textInputs[count]);
    count++;
}

我如何以字符串或任何其他形式访问文本输入数组的第一个、第二个以进一步进行

【问题讨论】:

  • textInputs[0].text 和 textInputs[1].text 有什么问题?

标签: apache-flex actionscript-3 mxml


【解决方案1】:

我不确定我是否理解您的问题,但您没有设置文本输入的值:

我更喜欢以下方式:

//make the array an instance variable instead of local var
//so that it can be accessed from other functions if required.
public var textInputs:Array = [];

for(var str:String in columnsData)
{
  var ti:TextInput = new TextInput();
  ti.text = str;
  addChild(ti);
  textInputs.push(ti);
}

您可以使用textInputs[0].text 等访问这些值。

if(textInput != null){
  for(var i:Number = 0; i < textInputs.length; i++)
    trace(textInputs[i].text);
}

【讨论】:

    【解决方案2】:
    (getChildAt(0) as TextInput).text //first
    (getChildAt(1) as TextInput).text //second
    

    如果您的精灵在索引 0 和 1 处包含除 TextInput 以外的其他内容,您将获得空引用异常。

    【讨论】:

    • 这个 getChildAt 属于..?
    • 公共函数 ConticlickHandler():void { if(contin.label == "Continue") { Alert.show("Inside"); //第一个 Alert.show(fieldInputs[0].text.toString());所以在这里我在单独的函数中访问 fieldInputs[0] 但我不能显示它 flex 脚本但是 Alert 中的内部消息来了为什么不 fieldinputs
    【解决方案3】:
    <?xml version="1.0"?>
    <!-- dpcontrols\UseIList.mxml -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        initialize="initData();">
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
    
        <fx:Script>
            <![CDATA[
                import mx.collections.*;
    
                // The data provider is an Array of Strings
                public var myArray:Array = ["AZ", "MA", "MZ", "MN", "MO", "MS"];
                // Declare an ArrayList that represents the Array.
                [Bindable]
                public var myAL:ArrayList;
    
                //Initialize the ArrayList.
                public function initData():void {
                    myAL = new ArrayList(myArray); 
                }
    
                // The function to change the collection, and therefore
                // the Array.
                public function changeCollection():void {
                    // Get the original collection length. 
                    var oldLength:int=myAL.length;
    
                    // Remove the invalid first item, AZ.
                    var removedItem:String=String(myAL.removeItemAt(0));
                    // Add ME as the second item. (ILists used 0-based indexing.)
                    myAL.addItemAt("ME", 1);
                    // Add MT at the end of the Array and collection.
                    myAL.addItem("MT");
                    // Change the third item from MZ to MI.
                    myAL.setItemAt("MI", 2);
                    // Get the updated collection length. 
                    var newLength:int=myAL.length;
                    // Get the index of the item with the value ME.
                    var addedItemIndex:int=myAL.getItemIndex("ME");
                    // Get the fifth item in the collection.
                    var index4Item:String=String(myAL.getItemAt(4));
    
                    // Display the information in the TextArea control.
                    ta1.text="Start Length: " + oldLength + ". New Length: " +
                        newLength;
                    ta1.text+=".\nRemoved " + removedItem;
                    ta1.text+=".\nAdded ME at index " + addedItemIndex;
                    ta1.text+=".\nThe item at index 4 is " + index4Item + ".";
                    // Show that the base Array has been changed.
                    ta1.text+="\nThe base Array is: " + myArray.join();
                }
            ]]>
        </fx:Script>
    
        <s:ComboBox id="myCB" dataProvider="{myAL}"/>
        <s:TextArea id="ta1" height="75" width="300"/> 
        <s:Button label="rearrange list" click="changeCollection();"/>
    </s:Application>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多