如何在cocos2d.Android cocos中添加文本输入字段?

时间:2015-08-27 18:14:26

标签: textbox cocos2d-android cocossharp

我试图让CCTextFieldTTF在Xamarin的cocos sharp中用于Android应用程序。但是对于我的生活来说无法掌握这一点。找不到关于cocos sharp API的任何文档。有谁知道如何使用这个类来呈现Android应用程序中的文本区域?我问的原因是在一个xamarin论坛,我看到有人说这在API中不起作用。任何帮助将受到高度赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我在android中有这个工作

以下是示例代码:

创建一个跟踪文本字段的节点

    CCTextField trackNode;
    protected CCTextField TrackNode
    {
        get { return trackNode; }
        set
        {
            if (value == null)
            {
                if (trackNode != null)
                {
                    DetachListeners();
                    trackNode = value;
                    return;
                }
            }

            if (trackNode != value)
            {
                DetachListeners();
            }

            trackNode = value;
            AttachListeners();
        }
    }

//创建实际输入文本域

var textField = new CCTextField(string.Empty, "Somefont", 25,  CCLabelFormat.SystemFont);
textField.IsColorModifiedByOpacity = false;
textField.Color = new CCColor3B(Theme.TextWhite);
textField.BeginEditing += OnBeginEditing;
textField.EndEditing += OnEndEditing;
textField.Position = new CCPoint (0, 0);
textField.Dimensions = new CCSize(VisibleBoundsWorldspace.Size.Width - (160 * sx),  vPadding);
textField.PlaceHolderTextColor = Theme.TextYellow;
textField.PlaceHolderText = Constants.TextHighScoreEnterNamePlaceholder;
textField.AutoEdit = true;
textField.HorizontalAlignment = CCTextAlignment.Center;
textField.VerticalAlignment = CCVerticalTextAlignment.Center;
TrackNode = textField;
TrackNode.Position = pos;

AddChild(textField);

// Register Touch Event
var touchListener = new CCEventListenerTouchOneByOne();
touchListener.OnTouchBegan = OnTouchBegan;
touchListener.OnTouchEnded = OnTouchEnded;
AddEventListener(touchListener);

//事件

        bool OnTouchBegan(CCTouch pTouch, CCEvent touchEvent)
        {
            beginPosition = pTouch.Location;
            return true;
        }

        void OnTouchEnded(CCTouch pTouch, CCEvent touchEvent)
        {
            if (trackNode == null)
            {
                return;
            }

            var endPos = pTouch.Location;

            if (trackNode.BoundingBox.ContainsPoint(beginPosition) && trackNode.BoundingBox.ContainsPoint(endPos))
            {
                OnClickTrackNode(true);
            }
            else
            {
                OnClickTrackNode(false);
            }
        }

        public void OnClickTrackNode(bool bClicked)
        {
            if (bClicked && TrackNode != null)
            {
                if (!isKeyboardShown) 
                {
                    isKeyboardShown = true;
                    TrackNode.Edit();   
                }

            }
            else
            {
                if (TrackNode != null)
                {
                    TrackNode.EndEdit();
                }
            }

        }

        private void OnEndEditing(object sender, ref string text, ref bool canceled)
        {
            //((CCNode)sender).RunAction(scrollDown);
            Console.WriteLine("OnEndEditing text {0}", text);
        }

        private void OnBeginEditing(object sender, ref string text, ref bool canceled)
        {
            //((CCNode)sender).RunAction(scrollUp);
            Console.WriteLine("OnBeginEditing text {0}", text);
        }

        void AttachListeners()
        {
            // Attach our listeners.
            var imeImplementation = trackNode.TextFieldIMEImplementation;
            imeImplementation.KeyboardDidHide += OnKeyboardDidHide;
            imeImplementation.KeyboardDidShow += OnKeyboardDidShow;
            imeImplementation.KeyboardWillHide += OnKeyboardWillHide;
            imeImplementation.KeyboardWillShow += OnKeyboardWillShow;
            imeImplementation.InsertText += InsertText;
        }

        void DetachListeners()
        {
            if (TrackNode != null)
            {
                // Remember to remove our event listeners.
                var imeImplementation = TrackNode.TextFieldIMEImplementation;
                imeImplementation.KeyboardDidHide -= OnKeyboardDidHide;
                imeImplementation.KeyboardDidShow -= OnKeyboardDidShow;
                imeImplementation.KeyboardWillHide -= OnKeyboardWillHide;
                imeImplementation.KeyboardWillShow -= OnKeyboardWillShow;
                imeImplementation.InsertText -= InsertText;
            }
        }

这一切都来自下面的链接,但需要一些额外的工作才能让它在每个平台上运行。 https://github.com/mono/cocos-sharp-samples/tree/master/TextField