代码之家  ›  专栏  ›  技术社区  ›  ggkmath

Flex:如何控制表单项和相应的errorString之间的间距?

  •  4
  • ggkmath  · 技术社区  · 12 年前

    这里有一个简单的应用程序来说明我的问题。说明:

    1. 运行附加的Flex 4代码
    2. 在第一个表单项目中输入0(例如“输入速度(MPH)”)
    3. 单击第二个表单项目
    4. 注意,错误字符串出现在第一个表单项的右侧

    问题:如何使错误字符串直接出现在第一个表单项的右侧,同时保持表单的宽度固定(例如,当前示例使用400像素的固定表单宽度)?

    <?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" minWidth="955" minHeight="600">
    
        <fx:Script>
            <![CDATA[
                private function validateSpeed():void {
                    var num:Number=Number(speedId.text);
                    speedId.errorString="";
    
                    if (speedId.text=="") {
                        speedId.errorString="This field is required.";  
                        return;
                    } else if ( Number(speedId.text)<1000) {
                        speedId.errorString="The speed must be at least 1 MPH.";
                        return;
                    } else if ( Number(speedId.text)>1e11) {
                        speedId.errorString="The speed cannot exceed 100 MPH.";
                        return;
                    }
                }       
            ]]>
        </fx:Script>
    
        <s:Form width="600" >   
            <s:layout>
                <s:FormLayout gap="-10" horizontalAlign="center"/>
            </s:layout> 
    
            <s:FormItem id="speedFI" label="Enter Speed (MPH):" width="600" required="true">
                <s:TextInput id="speedId" width="195" textAlign="right"
                         restrict="0-9" maxChars="7"
                         focusOut="validateSpeed();"
                         toolTip="Enter a speed between 1 and 100 MPH."/>
            </s:FormItem>   
    
            <s:FormItem id="dummyFI" label="Dummy Label:" width="600" required="true">
                <s:TextInput id="dummyId" width="195" textAlign="center"
                         restrict="0-9" maxChars="7" prompt="Click here after entering 0 above."
                         toolTip="Dummy form item."/>
            </s:FormItem>           
        </s:Form>
    </s:Application>
    
    1 回复  |  直到 12 年前
        1
  •  3
  •   Sunil D.    12 年前

    您可以通过自定义来控制错误消息的位置 FormItemSkin .

    以下是默认设置中的一些片段 表单项目外观 班请注意,此蒙皮使用“约束”列/行来排列各个部分。您可以修改列的定义,也可以修改“helpColumn”中错误消息的位置。有很多其他方法可以解决这个问题,但看起来你需要在皮肤内部做到这一点。

    约束列/行声明:

    <s:layout>
        <s:FormItemLayout>
            <s:constraintColumns>
                <!--- The column containing the sequence label. -->
                <s:ConstraintColumn id="sequenceCol" />
                <!--- The column containing the FormItem's label. -->
                <s:ConstraintColumn id="labelCol" />
                <!--- The column containing the FormItem's content. -->
                <s:ConstraintColumn id="contentCol" width="100%"/>
                <!--- The column containing the FormItem's help content. -->
                <s:ConstraintColumn id="helpCol" maxWidth="200"/>
            </s:constraintColumns>         
            <s:constraintRows>
                <!--- @private -->
                <s:ConstraintRow id="row1" baseline="maxAscent:10" height="100%"/>
            </s:constraintRows>  
        </s:FormItemLayout>
    </s:layout>
    

    这是用于显示错误消息的标签对象。您可以使用以下方法将标签的左边缘设置为更靠近输入字段: left="helpCol:10" (而不是 helpCol:27 ).

    <s:RichText id="errorTextDisplay" includeIn="errorStates"
                fontStyle="italic" fontWeight="normal" color="0xFE0000"
                left="helpCol:27" right="helpCol:10"
                bottom="row1:10" baseline="row1:0" 
                maxDisplayedLines="-1"/>