【问题标题】:How to alter the active text field in AS3?如何更改 AS3 中的活动文本字段?
【发布时间】:2014-06-08 23:55:34
【问题描述】:

我在舞台上有 6 个文本字段以及一个带有数字 0-9 的键盘。

最终这将是一款 Android 应用。我希望能够选择一个文本字段,然后按小键盘按钮并使数字出现在活动文本字段中。

我一直在尝试 Google 活动字段和类似搜索,但似乎找不到参考。

请记住,我试图从多个教程中收集到的东西,我在黑暗中摸索。这段代码可能完全是垃圾:

package  {

public class main {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import fl.managers.FocusManager;
    var focus:FocusManager = new FocusManager(this);

    btn_1.addEventListener(MouseEvent.MOUSE_DOWN, inputNumber);

    public function inputNumber(m:MouseEvent){
            focus.text = 1;
    }

    public function main() {
        // constructor code
    }

}

}

当前错误:

C:\Users\Otaku\Documents\lottocount\main.as, Line 19, Column 35 1118: Implicit coercion of a value with static type flash.display:InteractiveObject to a possibly unrelated type flash.text:TextField.

C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 44 1120: Access of undefined property onMyButtonClick.

C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 3  1120: Access of undefined property btn_1.

C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 47 1120: Access of undefined property onFocusIn.

C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 3  1120: Access of undefined property stage.

【问题讨论】:

    标签: android actionscript-3 textfield


    【解决方案1】:

    如果您访问 focus 对象,您将获得对当前活动(聚焦)对象的引用。

    但实际上,只要有一个允许输入的 TextField 就会在触摸它时打开一个键盘视图(仅在移动设备上)。

    您还可以告诉 Flash 不要自动显示键盘,如果您打算使用自己的键盘,那么您可以使用焦点或制作自己的系统,该系统将根据最后一次触摸的位置跟踪适当的 TextField。

    编辑:

    这里有一个简单的例子来说明如何做到这一点。
    在此示例中,我有两个 TextField:tf1 和 tf2。此外,我还添加了一个按钮,以涵盖需要将最后一个焦点保存在具体类型的对象上的情况。按钮名称是 myButton。

    var lastSelectedTextField:TextField; // this will hold our last selected TextField
    
    // make sure stage exists. If you're writing script in a frame, don't mind, if you use OOP approach you can do so by adding an eventListener for the event ADDED_TO_STAGE
    
    stage.addEventListener(FocusEvent.FOCUS_IN, onFocusIn); // onFocusIn will trigger every time a focus is changed.
    myButton.addEventListener(MouseEvent.CLICK, onMyButtonClick);
    
    function onFocusIn(e:FocusEvent):void
    {
        if (stage.focus is TextField)
               lastSelectedTextField = stage.focus as TextField;
    }
    
    function onMyButtonClick(e:MouseEvent):void
    {
        trace("Text of the last selected text field is:", lastSelectedTextField.text);
    }
    

    【讨论】:

    • 感谢您的回复!不过我还是有点困惑。当我打开多个文本字段并单击一个时,AS3 会自动跟踪当前焦点吗?在这种情况下,我只需要对数字按钮进行编码即可将数字输入到当前焦点中?
    • 是的,Flash 会跟踪当前焦点,还有一些事件会通知您的对象获得或失去焦点。请注意,单击按钮将使按钮获得焦点;)您可能希望设置一个条件,即仅当当前选择是 TextField 类型或您要使用的其他类时才会发生输入。
    • 我很头疼要让它工作。如果我能得到一个部分例子,我想我可以开始做得更好。等一下,我会把我的部分代码上传到我的主帖。
    • 我将为您制作一个带有 2 个文本字段和一个按钮的示例,但在我完成这里的工作之后;)您可能需要等待 1 小时。
    • 别担心,感谢您愿意帮助一个失落的可怜新手。 :)
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2012-11-28
    • 2013-04-24
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多