0

我正在尝试让 CCTextFieldTTF 与 Xamarin 一起在 cocos 中工作,以用于 android 应用程序。但我这辈子都抓不到。也找不到关于 cocos sharp API 的任何文档。有谁知道如何使用此类在 android 应用程序中呈现文本区域?我问的原因是在一个 xamarin 论坛中,我看到有人说这在 API 中还不起作用。任何帮助将不胜感激。提前致谢。

4

1 回答 1

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

于 2016-06-13T22:17:07.037 回答