这是一个简单的应用程序来说明我的问题。指示:
- 运行附加的 Flex 4 代码
- 在第一个表单项中输入 0(例如“输入速度 (MPH)”)
- 单击第二个表单项
- 观察错误字符串出现在第一个表单项的右侧
问题:如何使错误字符串直接出现在第一个表单项的右侧,同时保持表单的宽度固定(例如,当前示例使用 400 px 的固定表单宽度)?
<?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>