zoukankan      html  css  js  c++  java
  • XNA游戏:软键盘弹窗输入

    在XNA中如果我们需要输入文字,那么我们就需要使用到软键盘了,在XNA中使用软键盘要用到Guide.BeginShowKeyboardInput方法,由于游戏的Update是会不断地执行的,所以要由Guide.IsVisible来检查弹出输入框是否已经显示出来了。

    Guide.BeginShowKeyboardInput方法的参数
    PlayerIndex     玩家的编号,手机是PlayerIndex.One
    Title     输入窗口的标题
    Description     输入窗口的描述
    DefaultText     默认的文字
    Callback     回调的方法
    State     使用者想要传送的物件

    Guide.BeginShowMessageBox时弹出一个窗口没有软键盘输入,这个方法的参数分別是
    Title     窗口的标题
    Text     窗口的文字
    Buttons     按钮
    FoucsButton     预设的按钮
    Icon     图标
    Callback     回调的方法
    State     使用者想要传送的物件

    示例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Input.Touch;
    using Microsoft.Xna.Framework.Media;
    
    namespace SIPSample
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
            SpriteFont spriteFont;
    
            string sipTitle = "This is the title.";
            string sipDescription = "This is the description that goes beneath the title.";
            string sipResult = "You type stuff here.";
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                TargetElapsedTime = TimeSpan.FromTicks(333333);
            }
    
            protected override void Initialize()
            {
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                spriteBatch = new SpriteBatch(GraphicsDevice);
                spriteFont = Content.Load<SpriteFont>("SpriteFont1");
            }
    
            protected override void UnloadContent()
            {
            }
            /// <summary>
            /// 输入完成回调方法
            /// </summary>
            /// <param name="result"></param>
            void keyboardCallback(IAsyncResult result)
            {
                string retval = Guide.EndShowKeyboardInput(result);
    
                if (retval != null)
                {
                    sipResult = retval;
                }
            }
    
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
                // Display the SIP
                TouchCollection touchCollection = TouchPanel.GetState();
    
                foreach (TouchLocation touch in touchCollection)
                {
                    if (touch.State == TouchLocationState.Pressed)
                        if (!Guide.IsVisible)
                            //弹出软键盘输入框
                            Guide.BeginShowKeyboardInput(PlayerIndex.One, sipTitle, sipDescription,
                                sipResult, keyboardCallback, new object());
                }
    
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                //绘制界面的文字
                spriteBatch.Begin();
                spriteBatch.DrawString(spriteFont, sipResult, new Vector2 { X = 50, Y = 200 }, Color.Black);
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }

  • 相关阅读:
    一定间隔时间下重复执行一个函数的几个方法
    关于 MonoDevelop on Linux 单步调试问题的解决
    MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记
    在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录
    在CentOS 6.4 x86_32中使用Rhythmbox听MP3
    MonoDevelop 4.0.9 on CentOS 6.3 安装笔记
    MemoryMappedFile 在 Mono in Linux 的开发笔记
    Mono on CentOS 6.3 安装笔记
    庆祝下:iOS 开发者企业级计划(299美元/年帐户+邓白氏码免费) 和 Windows Phone公司应用(公司帐户99美元+Symantec企业证书299美元/年))顺利发布成功
    PowerDesigner中NAME和COMMENT的互相转换,需要执行语句
  • 原文地址:https://www.cnblogs.com/linzheng/p/2450598.html
Copyright © 2011-2022 走看看