如何在按下 Enter 后将焦点和光标保持在 Flex 的 TextInput 中?

发布于 2024-07-29 11:18:29 字数 1836 浏览 1 评论 0 原文

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

这里有一个屏幕截图的链接: 屏幕截图

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

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

这样就可以了。

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

如果您键入一些内容,然后单击“添加到购物车”按钮,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"/>

I am a Flex newbie and I am testing a little application that simulates a cart. (It is based on a nice sample by Farata Systems' Yakov Fain).
Note: I am using the beta of Flash Builder 4 to code the application.

Here you have a link to the screenshot:
Screenshot

(Sorry I can't insert the image of the screenshot right here since stackoverflow doesn't allow new users to use image tags.)

The application is very simple. You type a product in the TextInput control and then click on the "Add to cart" button to add it to the "cart" which is represented by the TextArea at the bottom.

That works ok.

The problem is that I also want the user to be able to keep adding items to the cart without having to click on the "Add to cart" button. So, I added code to handle the enter event of the TextInput by calling the same handler function triggered by the "Add to cart" Click event.

If you type some content and then click the "Add to cart" button, the TextInput control receives the focus and the cursor, so you can type again.
However, if you hit enter, instead of clicking the button, the TextInput control keeps focused and you can see the cursor beam, but you can not enter text until you click elsewhere and come back to the control.

Below you can see the relevant part of the code, with the definition of the component that groups the three controls at the top (Label, TextInput, Button).

¿Any suggestions?

<?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"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┈┾☆殇 2024-08-05 11:18:29

首先感谢丹的回答。 我尝试了一下,但没有成功。

然而,丹的帖子为我指明了正确的方向。

首先,为了让您处于更好的上下文中,让我展示一下主 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 控件获得了焦点。

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

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>

first thanks to dan for his answer. I tried it, and it didn't work.

However, dan's post pointed me to the right direction.

First, to place you in a better context, let me show the main mxml file (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>

I think the problem is that the component grouping the Label, TextInput and Button -called NewItem- didn't get the focus, although the TextInput control did.

So, I simply added a call to this.SetFocus, to set the focus to the NewItem component, before setting the focus to the TextInput control.

The source code for the working version of NewItem is this (look for the //Solution comments to find the changes):

<?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>
悲凉≈ 2024-08-05 11:18:29

我认为你的问题是,一旦你按下回车键,光标就会返回到 HTML 页面。 因此,当播放器在正确的控件周围显示其焦点矩形时,浏览器已再次恢复光标焦点。 解决方案是通过调用此处概述的一些简单的 javascript 来强制浏览器交还播放器控制权:

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

I think your issues is that the once you hit enter the cursor returns the to HTML page. So, while the player is showing it's focus rectangle around the correct control, the browser has gotten the cursor focus back again. A solution is to force the browser to give the Player control back by calling some simple javascript outlined here:

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

你与昨日 2024-08-05 11:18:29

jfc 的答案对我有用。 我有一个 Mate ListenerInjector 调用此例程以将焦点设置在带有 id="answerText" 的 TextInput 上。 如果没有 jfc 建议的 this.setFocus()TextInput 将显示蓝色轮廓,就好像它具有焦点,但没有光标,并且输入不会出现在那里。

public function readyForNextAnswer(e:Event) : void {
    this.setFocus()
    answerText.setFocus() // Tried focusManager.setFocus(answerText), too
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文