Flex - 組合框控制元件



當用戶從組合框控制元件的下拉列表中選擇一個專案時,資料專案會顯示在控制元件的提示區域中。

組合框和下拉列表控制元件之間的一個區別在於,組合框控制元件的提示區域是使用文字輸入控制元件實現的,而下拉列表控制元件的提示區域是標籤控制元件。因此,使用者可以編輯控制元件的提示區域以輸入一個不是可用選項之一的值。

類宣告

以下是spark.components.ComboBox類的宣告:

public class ComboBox 
   extends DropDownListBase 
      implements IIMESupport

公共屬性

序號 屬性和描述
1

enableIME : 布林值

[只讀]

2

imeMode : 字串

3

itemMatchingFunction : 函式 = null

指定一個回撥函式,用於在使用者在提示區域輸入字元時搜尋專案列表。

4

labelToItemFunction : 函式

指定一個回撥函式,用於將輸入到提示區域的新值轉換為與資料提供程式中的資料項相同的資料型別。

5

maxChars : 整數

提示區域可以包含的最大字元數(使用者輸入)。

6

openOnInput : 布林值 = true

如果為 true,則當用戶編輯提示區域時,下拉列表將開啟。

7

prompt : 字串

如果/當輸入文字為空時顯示的文字。

8

restrict : 字串

指定使用者可以輸入到提示區域的字元集。

公共方法

序號 方法和描述
1

ComboBox()

建構函式。

繼承的方法

此類繼承自以下類:

  • spark.components.supportClasses.DropDownListBase
  • spark.components.List
  • spark.components.supportClasses.ListBase
  • spark.components.SkinnableDataContainer
  • spark.components.supportClasses.SkinnableContainerBase
  • spark.components.supportClasses.SkinnableComponent
  • mx.core.UIComponent
  • mx.core.FlexSprite
  • flash.display.Sprite
  • flash.display.DisplayObjectContainer
  • flash.display.InteractiveObject
  • flash.display.DisplayObject
  • flash.events.EventDispatcher
  • Object

Flex 組合框控制元件示例

讓我們按照以下步驟,透過建立一個測試應用程式來檢查在 Flex 應用程式中組合框控制元件的使用:

步驟 描述
1 如“Flex - 建立應用程式”一章所述,建立一個名為HelloWorld的專案,該專案位於包com.tutorialspoint.client下。
2 修改HelloWorld.mxml,如下所述。保持其餘檔案不變。
3 編譯並執行應用程式,以確保業務邏輯按要求工作。

以下是修改後的 mxml 檔案src/com.tutorialspoint/HelloWorld.mxml的內容

<?xml version = "1.0" encoding = "utf-8"?>
<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"
   width = "100%" height = "100%" minWidth = "500" minHeight = "500">
   
   <fx:Style source = "/com/tutorialspoint/client/Style.css" />
   <fx:Script>
      <![CDATA[
         import mx.collections.ArrayCollection; 

         [Bindable]
         public var data:ArrayCollection = new ArrayCollection (
         [   
            {value:"France", code:"FR"},
            {value:"Japan", code:"JP"},
            {value:"India", code:"IN"},
            {value:"Russia", code:"RS"},
            {value:"United States", code:"US"}		
         ]                
         );

         private function mappingFunction(input:String):Object {
            switch (input){
               case "France":
                  return {value:input, code:"FR"};		
               case "Japan":
                  return {value:input, code:"JP"};		
               case "India":
                  return {value:input, code:"IN"};		
               case "Russia":
                  return {value:input, code:"RS"};		
               case "United States":
                  return {value:input, code:"US"};		
            }			
            return null;
         }

      ]]>
   </fx:Script>
   
   <fx:Declarations>
      <mx:DateFormatter id = "dateFormatter" />
   </fx:Declarations>
   
   <s:BorderContainer width = "550" height = "400" id = "mainContainer"
      styleName = "container">
      <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center"
         verticalAlign = "middle">
         <s:Label id = "lblHeader" text = "Form Controls Demonstration" 
            fontSize = "40" color = "0x777777" styleName = "heading" />
         
         <s:Panel id = "comboBoxPanel" title = "Using ComboBox" width = "420" 
            height = "200">
         <s:layout>
            <s:VerticalLayout  gap = "10" verticalAlign = "middle" 
               horizontalAlign = "center" />	
         </s:layout>
         
         <s:HGroup>
            <s:Label text = "Index :" /> <s:Label 
               text = "{comboBox.selectedIndex}" fontWeight = "bold" />
            <s:Label text = "Value :" /> <s:Label 
               text = "{comboBox.selectedItem.value}" fontWeight = "bold" />
            <s:Label text = "Code :" /> <s:Label 
               text = "{comboBox.selectedItem.code}" fontWeight = "bold" />
         </s:HGroup>
         
         <s:ComboBox  id = "comboBox"  dataProvider = "{data}" width = "150" 
            labelToItemFunction = "{mappingFunction}" selectedIndex = "0" 
            labelField = "value" />     
         </s:Panel>
      </s:VGroup>	 
   </s:BorderContainer>	
</s:Application>

完成所有更改後,讓我們像在Flex - 建立應用程式章節中那樣,以普通模式編譯並執行應用程式。如果應用程式一切正常,它將產生以下結果:[ 線上試用 ]

Flex ComboBox Control
flex_form_controls.htm
廣告
© . All rights reserved.