Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
885,6 KB
Nội dung
Chapter 8: Using Flex Visual Controls
271
groupName=”buttonGroup”/>
</s:VGroup>
</s:Application>
On the Web
The code in Listing 8.4 is available in the Web site files in the chapter08 project as RadioButtonDemo.
mxml
.
n
Figure 8.10 shows the application displaying the resulting RadioButton controls and a pop-up
window created by the
Alert class that’s displayed when the user clicks a RadioButton or oth-
erwise selects a new value.
Tip
The RadioButtonGroup control dispatches the change event whenever its selectedValue property
changes. It also dispatches an
itemClick event when the user clicks on any of the group’s member objects.
n
FIGURE 8.10
An application with RadioButton controls
Other Data Entry Controls
The Flex framework includes these other controls that can be used to collect data from the applica-
tion’s user:
l
NumericStepper
l
DateField
l
DateChooser
l
ColorPicker
14_488959-ch08.indd 27114_488959-ch08.indd 271 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part II: Designing Flex Applications
272
Each of these controls is designed to support data entry for a particular type of data.
Tip
In the Flex4 SDK, the NumericStepper control has been rewritten as a Spark component. The MX versions
of the
DateField, DateChooser, and ColorPicker controls are the most recent versions and can be used
in Flex4 applications.
n
The NumericStepper control
The NumericStepper is a compound control that’s designed for numeric data entry. It includes
a
TextInput control for direct entry and a set of buttons that increment and decrement the con-
trol’s current value.
The
NumericStepper doesn’t have its own label property, so it’s typically paired with a
Label control or wrapped in a FormItem container, which has a label property. This code
declares a simple
NumericStepper wrapped in an HGroup with a Label:
<s:HGroup>
<s:Label text=”Enter value:”/>
<s:NumericStepper id=”myStepper” value=”5”/>
</s:HGroup>
As shown in Figure 8.11, the control displays its value property, and the user can change it.
FIGURE 8.11
A NumericStepper control
The NumericStepper supports these properties that determine its behavior:
l
minimum. The minimum permitted value; defaults to 0.
l
maximum. The maximum permitted value; defaults to 10.
l
stepSize. The amount to increment or decrement when the control’s buttons are clicked;
defaults to
1.
l
maxChars. The maximum length of the value that can be directly typed into the control.
14_488959-ch08.indd 27214_488959-ch08.indd 272 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 8: Using Flex Visual Controls
273
This NumericStepper has a minimum value of 5, a maximum value of 25, and a stepSize
of 5:
<mx:NumericStepper id=”myStepper”
minimum=”5” maximum=”25” stepSize=”5”/>
The NumericStepper control’s value property is bindable and can be used in a binding
expression or ActionScript statement to get the value the user has entered:
<s:Label text=”You entered: {myStepper.value}”/>
Date controls
Two data entry controls are designed to show or select a date value:
l
DateChooser. Displays a calendar from which the user selects a date.
l
DateField. Displays a TextInput and a small calendar icon. When either is clicked, a
calendar is displayed for date selection.
The DateChooser control
The DateChooser control presents an interactive calendar that displays a month and year and
enables the user to do the following:
l
Navigate forward and back one month at a time
l
Select a single date, multiple dates, or a range of dates with mouse operations
The following code declares a simple
DateChooser control:
<mx:DateChooser id=”myDateChooser”/>
The DateChooser control supports Boolean properties named allowMultipleSelection
and
allowDisjointSelection that respectively enable a user to select multiple and non-con-
tiguous dates. Changing either property causes changes in the control’s visual presentation.
As shown in Figure 8.12, the
DateChooser is presented as a visual calendar from which the user
makes selections.
The DateField control
The DateField control presents the user with an input control and a small calendar icon. By
default, when the user clicks either the icon or the input, a calendar control pops up that looks the
same as the
DateChooser and enables the user to make his selection. However, unlike the
DateChooser component, only a single date value can be selected.
14_488959-ch08.indd 27314_488959-ch08.indd 273 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part II: Designing Flex Applications
274
FIGURE 8.12
A DateChooser control
The following code declares a simple DateField control:
<mx:DateField id=”myDateField”/>
As shown in Figure 8.13, the DateField is presented as an input control and icon which, when
clicked, present a calendar control.
The
DateField control has a Boolean property named editable that’s set to false by
default. When set to
true, the user can click into the input area and type a date value.
FIGURE 8.13
A DateField control
14_488959-ch08.indd 27414_488959-ch08.indd 274 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 8: Using Flex Visual Controls
275
Date entry properties and methods
The DateChooser and DateField controls share a common set of properties that enable you
to control their behavior and collect their data. Table 8.4 describes these properties and their
capabilities.
TABLE 8.4
Date Entry Control Properties
Property Data Type Description Default
selectedDate Date
The currently selected date value.
null
showToday Boolean
Determines whether the current date
is highlighted.
true
dayNames Array
An array of String values used as
labels for the day names.
[ “S”, “M”, “T”, “W”,
“T”, “F”, “S” ]
minYear Int
The minimum allowed year.
1900
maxYear Int
The maximum allowed year.
2100
disabledDays Array
An array of integer values indicating
by zero-based index days that aren’t
selectable.
[]
Setting of [0,6] would dis-
able Sunday and Saturday
disabledRanges Array of
Object
A set of disabled ranges. Each range
has named properties of
range-
Start
and rangeEnd typed as
Date values.
[]
selectableRange Object
A selectable range. Requires named
properties of
rangeStart and
rangeEnd typed as Date values.
null
Other useful properties are described in the API documentation for DateField and
DateChooser.
The ColorPicker control
The ColorPicker control enables a user of your application to select from one of standard “web-
safe” colors. It displays a button and, when clicked, a palette of colors. The currently selected color
is represented by the control’s
selectedColor property, which returns a uint (unsigned inte-
ger) value. You can pass the value of the control’s
selectedColor to any other style or property
which expects a color value.
Note
The ColorPicker control has not been rewritten in the Spark component framework for Flex4. The MX ver-
sion of the control, however, works fine in Flex4 applications.
n
14_488959-ch08.indd 27514_488959-ch08.indd 275 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part II: Designing Flex Applications
276
The application in Listing 8.5 displays a ColorPicker control. When the user selects a color,
a
change event is dispatched. The code in the event handler function sets the application’s
backgroundColor style to the color that’s selected by the application user:
LISTING 8.5
An application using a color picker
<?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”
backgroundColor=”#EEEEEE”>
<fx:Script>
<![CDATA[
import mx.events.ColorPickerEvent;
protected function colorPicker_changeHandler(
event:ColorPickerEvent):void
{
this.setStyle(“backgroundColor”, event.target.selectedColor);
}
]]>
</fx:Script>
<s:Panel title=”Using the ColorPicker control”
horizontalCenter=”0” top=”20” width=”300”>
<s:layout>
<s:HorizontalLayout paddingTop=”20” paddingRight=”10”
paddingLeft=”20” paddingBottom=”10”/>
</s:layout>
<s:Label text=”Choose an application background color:”/>
<mx:ColorPicker id=”colorPicker” selectedColor=”#EEEEEE”
change=”colorPicker_changeHandler(event)”/>
</s:Panel>
</s:Application>
On the Web
The code in Listing 8.5 is available in the Web site files in the chapter08 project as ColorPickerDemo.
mxml
.
n
Figure 8.14 shows the resulting application, with a ColorPicker control inside a Panel.
14_488959-ch08.indd 27614_488959-ch08.indd 276 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 8: Using Flex Visual Controls
277
FIGURE 8.14
The ColorPicker control
Using Interactive Controls
Beyond the data entry controls I described previously, certain controls are designed for user inter-
action that can be used in a variety of applications. In this section, I describe the
ScrollBar and
Slider controls.
The ScrollBar controls
There are two versions of the ScrollBar control:
l
HScrollBar. For a horizontal scrollbar.
l
VScrollBar. For a vertical scrollbar.
A
ScrollBar control has four graphic elements: a track, a button, and two arrows. The user
changes the control’s current value by clicking and dragging the button, clicking above or below
the button, or clicking one of the arrows. The
ScrollBar returns its current value through its
scrollPosition property. The scrollPosition property isn’t bindable, so typically it han-
dles
ScrollBar interactions by listening for the scroll event, which is dispatched each time the
position of the button changes.
Cross-Reference
The Flex4 SDK also includes a new Scroller component that’s designed to enable scrolling in Spark contain-
ers such as the new version of
Panel. I describe how to use the Scroller component in Chapter 10.
n
14_488959-ch08.indd 27714_488959-ch08.indd 277 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part II: Designing Flex Applications
278
ScrollBar properties
The new Spark versions of the VScrollBar and HScrollBar controls are extended from the
ScrollBar superclass, which implements the properties described in Table 8.5.
TABLE 8.5
ScrollBar Properties
Property Data Type Description Default
Value Number
The position of the scroll button relative to the top of a
VScrollBar or the left of an HScrollBar. This property is
bindable.
null
Minimum Number
The minimum value of scrollPosition. 0
Maximum Number
The maximum value of scrollPosition. 100
pageSize Number
Determines delta of change in pixels when user clicks before
or after the scroll button.
20
The change event
The change event is dispatched each time the user interacts with the ScrollBar control. Its
event object is typed as an event class named
mx.events.ScrollEvent, which has a posi-
tion
property containing the new scrollPosition.
In the application in Listing 8.6, the
HScrollBar control’s new scrollPosition is displayed
in a
Label control whose text property is changed each time the scroll event is handled:
LISTING 8.6
An application using a scroll bar
<?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”>
<s:layout>
<s:VerticalLayout horizontalAlign=”center” paddingTop=”20”/>
</s:layout>
<fx:Script>
<![CDATA[
[Bindable]
protected var scrollPos:Number;
protected function myScrollBar_changeHandler(event:Event):void
{
scrollPos = event.target.value;
}
14_488959-ch08.indd 27814_488959-ch08.indd 278 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 8: Using Flex Visual Controls
279
]]>
</fx:Script>
<s:Label id=”scrollLabel” fontSize=”18” fontWeight=”bold”
text=”Current scroll position: {scrollPos}”/>
<s:HScrollBar id=”myScrollBar” width=”300”
minimum=”0” maximum=”300” pageSize=”100”
change=”myScrollBar_changeHandler(event)”/>
</s:Application>
On the Web
The code in Listing 8.6 is available in the Web site files in the chapter08 project as RadioButtonDemo.
mxml
.
n
Figure 8.15 shows the HScrollBar and Label controls.
FIGURE 8.15
An HScrollBar and a Label control displaying its current position
The Slider controls
There are two versions of the Slider control:
l
HSlider. For a horizontal slider.
l
VSlider. For a vertical slider.
A
Slider control displays a track and a “thumb” graphic that enables the user to select a value by
clicking and dragging the thumb. You allow the slider to select any value within a range or restrict
it to selecting values at particular intervals. The control also can display two thumb icons to repre-
sent starting and ending values.
The user interacts with the
Slider control by clicking and dragging the thumb icon or by click-
ing before or after the thumb. If the user clicks before or after the thumb, the slider slides to the
selected position. If the
Slider has implemented snapping through the snapInterval prop-
erty, the thumb slides to the snapping position that’s closest to where the mouse click occurred.
14_488959-ch08.indd 27914_488959-ch08.indd 279 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part II: Designing Flex Applications
280
The Slider controls return their current value through the value property. The value prop-
erty is bindable, so you can handle
Slider interactions through either binding expressions or
events. Each time the
Slider control’s value changes, it dispatches the change event.
Slider properties
The VSlider and HSlider are extended from the Slider superclass, which implements the
properties described in Table 8.6.
TABLE 8.6
Slider Properties
Property Data Type Description Default
value Number The currently selected value of the Slider based on
thumb position. Relevant only when
thumbCount is 1.
0
minimum Number
Minimum value of the Slider. 0
maximum Number
Maximum value of the Slider. 10
snapInterval Number
When set a value other than 0, enforces snapping to par-
ticular intervals between minimum and maximum. If set
to 0, sliding is continuous.
0
The application in Listing 8.7 declares a horizontal Slider. Its value is displayed in a Label
control through a binding expression.
LISTING 8.7
An application using a slider
<?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”
creationComplete=”executeBindings(true)”>
<s:layout>
<s:VerticalLayout horizontalAlign=”center” paddingTop=”50”/>
</s:layout>
<s:HSlider id=”mySlider” width=”300”
minimum=”0” maximum=”300”
snapInterval=”50”/>
<s:Label fontSize=”18” fontWeight=”bold”
text=”Current slider position: {mySlider.value}”/>
</s:Application>
14_488959-ch08.indd 28014_488959-ch08.indd 280 3/5/10 2:25 PM3/5/10 2:25 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
[...]... source=”graphics/flower1.jpg” height=”200” width= 40 0” maintainAspectRatio=”false”/> Figure 8.18 shows the image with explicit height and width properties and maintain AspectRatio set to false 283 Part II: Designing Flex Applications FIGURE 8.18 An image with specific width and height and maintainAspectRatio set to false Embedding images When you embed an image in a Flex application, you expand the size of the application... potentially assisting with initial application load times n Note The FlashBuilder installation contains a file named flash- unicode-table.xml in the sdks /4. 0.0/ framesworks folder This file contains definitions of common Unicode character ranges The file is not processed with the command-line compiler or Flash Builder, but it can serve as a handy reference to common Unicode ranges n Declaring embedded fonts... classes Part II: Designing Flex Applications Using Advanced Text Layout Flex4 applications require Flash Player 10, whether the application is deployed over the Web or on the desktop with Adobe AIR One of the benefits of this most recent version of Flash Player is its capability to present complex text with an element of the software known as the Flash Text Engine (FTE) and the Text Layout Framework... be used in Flex applications: l Device fonts Typefaces that are installed on the client system l Embedded fonts Typefaces that are embedded in a compiled Flex application and delivered to the client system as part of the application SWF file Table 9.2 lists the pros and cons of using device versus embedded fonts 295 Part II: Designing Flex Applications TABLE 9.2 Pros and Cons of Device and Embedded... as database on a server), and stored in memory as constants or variables When text is presented to the user in visual control, you select many font settings, including the font typeface and its size, weight, and style In this chapter, I describe the various tools available for text processing and presentation in Flex I describe these strategies and techniques: l Using the Flash Text Engine (FTE) to... TextInput, and TextArea l Layout controls include HRule, VRule, and Spacer l Button controls include Button, CheckBox, RadioButton, and PopupButton l Other data entry controls include NumericStepper, DateField, DateChooser, and ColorPicker l Other interactive controls include HScrollBar, VScrollBar, HSlider, and VSlider l The MX Image control displays images that are loaded at runtime or embedded in the Flex. .. initially described in Chapter 8, support the features of the FTE and TLF: l Label l RichText l RichEditableText l TextInput l TextArea The FTE is a set of classes that support complex text presentation These classes are a part of Flash Player 10 and are available to all Flash documents regardless of whether they’re built with the Flex SDK or the Flash authoring environment The TLF is a separate framework,... classes packaged in the component library file textLayout.swc This SWC file is included with the Flex4 SDK, so its classes are available to all Flex4 applications Presenting richly formatted text As I previously described in Chapter 8, the RichText, RichEditableText, and TextArea controls support the content and textFlow properties, which you can use to describe richly formatted text When you set the... discussing using embedded images as Button control icons n 2 84 Chapter 8: Using Flex Visual Controls New Feature Flex4 adds two elements named and that can be used to define reusable visual elements in an MXML file The following application defines a graphical element named FlowerImage based on a BitmapImage and then presents it twice: ... transparency for both device and embedded fonts n Note You can embed other font styles such as PostScript Type 1 or bitmap fonts, but these fonts must first be embedded in a Flash document to vectorize them, and only then can they be embedded in a Flex application n Caution Fonts that you’ve downloaded or purchased from a font vendor aren’t always licensed for use in a Flash document or Flex application Check . component framework for Flex 4. The MX ver-
sion of the control, however, works fine in Flex 4 applications.
n
14_ 488959-ch08.indd 275 14_ 488959-ch08.indd 275. click into the input area and type a date value.
FIGURE 8.13
A DateField control
14_ 488959-ch08.indd 2 741 4 _48 8959-ch08.indd 2 74 3/5/10 2:25 PM3/5/10 2:25