简体   繁体   English

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

[英]How can you keep focus and cursor in Flex's TextInput after hitting enter?

I am a Flex newbie and I am testing a little application that simulates a cart. 我是Flex新手,正在测试一个模拟购物车的小应用程序。 (It is based on a nice sample by Farata Systems' Yakov Fain). (它是基于Farata Systems的Yakov Fain的一个很好的示例)。 Note: I am using the beta of Flash Builder 4 to code the application. 注意:我正在使用Flash Builder 4的beta版对该应用程序进行编码。

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.) (抱歉,我无法在此处插入屏幕截图的图像,因为stackoverflow不允许新用户使用图像标签。)

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. 您在TextInput控件中键入产品,然后单击“添加到购物车”按钮以将其添加到由底部TextArea表示的“购物车”中。

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. 因此,我通过调用“添加到购物车” Click事件触发的同一处理函数,添加了用于处理TextInput的enter事件的代码。

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. 如果键入一些内容,然后单击“添加到购物车”按钮,则TextInput控件将接收焦点和光标,因此您可以再次键入。 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. 但是,如果您按Enter键,则TextInput控件将保持焦点,并且可以单击光标,而不是单击按钮,但是可以看到光标,但是只有其他位置单击并返回到控件之后, 才能输入文本

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). 在下面,您可以看到代码的相关部分,以及在顶部将三个控件(标签,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"/>

first thanks to dan for his answer. 首先感谢dan的回答。 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): 首先,为了让您处于更好的环境中,让我显示主要的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>

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. 我认为问题在于,尽管TextInput控件确实将标签,TextInput和Button分组的组件( 称为NewItem )没有获得焦点。

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. 因此,在将焦点设置为TextInput控件之前,我仅向this.SetFocus添加了一个调用,以将焦点设置为NewItem组件。

The source code for the working version of NewItem is this (look for the //Solution comments to find the changes): 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>

I think your issues is that the once you hit enter the cursor returns the to HTML page. 我认为您的问题是,一旦您击中输入光标,就会返回HTML页面。 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: 一种解决方案是通过调用此处概述的一些简单的JavaScript来强制浏览器将播放器控件交还给用户:

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

jfc's answer worked for me. jfc的答案对我有用。 I have a Mate ListenerInjector calling this routine to set focus on the TextInput with id="answerText" . 我有一个Mate ListenerInjector调用此例程以将焦点设置为id="answerText"的TextInput上。 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. 没有jfc建议的this.setFocus()TextInput将显示一个蓝色轮廓,好像它具有焦点,但是没有光标,并且输入也不会在那里出现。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM