0

我是 Flex 新手,我正在测试一个模拟购物车的小应用程序。(它基于 Farata Systems 的 Yakov Fain 的一个很好的样本)。注意:我正在使用 Flash Builder 4 测试版来编写应用程序。

在这里你有一个截图的链接: 截图

(抱歉,我无法在此处插入屏幕截图的图像,因为 stackoverflow 不允许新用户使用图像标签。)

该应用程序非常简单。您在 TextInput 控件中键入产品,然后单击“添加到购物车”按钮将其添加到底部的 TextArea 表示的“购物车”中。

这行得通。

问题是我还希望用户能够继续将商品添加到购物车,而无需单击“添加到购物车”按钮。因此,我添加了代码来处理 TextInput 的输入事件,方法是调用由“添加到购物车”Click 事件触发的相同处理函数。

如果您键入一些内容,然后单击“添加到购物车”按钮,TextInput 控件将接收焦点和光标,因此您可以再次键入。但是,如果您按 Enter 键,而不是单击按钮,TextInput 控件将保持焦点,并且您可以看到光标光束,但您无法输入文本,直到您单击其他位置并返回控件。

您可以在下面看到代码的相关部分,其中定义了将三个控件组合在顶部(Label、TextInput、Button)的组件的定义。

有什么建议么?

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[           
        protected function newItemHandler():void
        {
            import cart.ItemAddedEvent; 
            var e : ItemAddedEvent = new ItemAddedEvent( "->" + textInputItemDescription.text );    
            textInputItemDescription.text = ""; 
            textInputItemDescription.setFocus();
            textInputItemDescription.setSelection(0,0); 
            dispatchEvent( e ); // Bubble to parent = true
            trace( "I just dispatched AddItemEvent " + e.toString() + "\n Content = " + e.itemDescription );
        }
    ]]>
</fx:Script>

<fx:Metadata>
    [Event(name="ItemAddedEvent",type="cart.ItemAddedEvent",bubbles=true)]
</fx:Metadata>
<mx:Label text="Item description:"/>
<s:TextInput id="textInputItemDescription" enter="newItemHandler() "/>
<s:Button click="newItemHandler()"  label="Add to cart"/>

4

3 回答 3

1

首先感谢丹的回答。我试过了,它没有用。

但是,丹的帖子为我指明了正确的方向。

首先,为了让您更好地了解上下文,让我展示主 mxml 文件 (SimpleCart.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/halo" 
               xmlns:ctrl="controls.*"
               xmlns:cart="cart.*"
               minWidth="1024" minHeight="768" >
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Style source="SimpleCart.css"/>
    <ctrl:NewItem id="buttonAddItem" x="9" y="15" width="394" height="27"/>     
    <cart:SmartShoppingCart  x="8" y="47" width="378"/>         
</s:Application>

我认为问题在于将 Label、TextInput 和 Button 分组的组件(称为 NewItem)没有获得焦点,尽管 TextInput 控件获得了焦点。

因此,在将焦点设置到 TextInput 控件之前,我只是添加了对 this.SetFocus 的调用,以将焦点设置到NewItem组件。

NewItem 的工作版本的源代码是这样的(查找 //Solution 注释以找到更改):

<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/halo" >

    <fx:Script>
        <![CDATA[   

            protected function newItemHandler():void
            {
                import cart.ItemAddedEvent; 
                import flash.external.ExternalInterface; 

                var e : ItemAddedEvent = new ItemAddedEvent( "->" + textInputItemDescription.text );    
                textInputItemDescription.text = "";

                // *** Solution begins here                     
                this.setFocus();
                // *** Solution ends here

                textInputItemDescription.setFocus();
                textInputItemDescription.setSelection(0,0); 
                dispatchEvent( e ); // Bubble to parent = true
            }
        ]]>
    </fx:Script>

    <fx:Metadata>
        [Event(name="ItemAddedEvent",type="cart.ItemAddedEvent",bubbles=true)]
    </fx:Metadata>

    <mx:Label text="Item description:"/>
    <s:TextInput id="textInputItemDescription" enter="newItemHandler() "/>
    <s:Button click="newItemHandler()"  label="Add to cart"/>
</s:HGroup>
于 2009-07-27T08:40:33.957 回答
0

我认为你的问题是一旦你点击输入光标就会返回到 HTML 页面。因此,当播放器在正确的控件周围显示它的焦点矩形时,浏览器又重新获得了光标焦点。一种解决方案是通过调用此处概述的一些简单 javascript 来强制浏览器将播放器控制权交还给播放器:

http://carrythezero.net/blog/2009/01/20/flex-textinput-focus-issues/

于 2009-07-24T16:16:34.583 回答
0

jfc's answer worked for me. I have a Mate ListenerInjector calling this routine to set focus on the TextInput with id="answerText". Without the this.setFocus() suggested by jfc, the TextInput will show a blue outline as if it has focus, but no cursor, and input does not appear there.

public function readyForNextAnswer(e:Event) : void {
    this.setFocus()
    answerText.setFocus() // Tried focusManager.setFocus(answerText), too
}
于 2012-09-04T19:18:19.560 回答