天天看點

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 圖示

示例

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);  

    }  

本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1078385

繼續閱讀