【问题标题】:Receiving input from Android soft keyboard in Monogame在 Monogame 中接收来自 Android 软键盘的输入
【发布时间】:2018-01-20 00:23:26
【问题描述】:

我只是想接收用户在使用 Android 系统键盘时按下的字符。 OSK 显示正常,但我无法接收用户输入的字符。我尝试了两种不同的方法(我能找到的只有两种)。

第一种方法是使用Activity 类中的View 对象,如here 所示。我无法让它工作,所以我一直在寻找和尝试不同的东西,但没有任何效果。

我的Activity 班级

[Activity(Label = "Game1"
    , MainLauncher = true
    , Icon = "@drawable/icon"
    , Theme = "@style/Theme.Splash"
    , AlwaysRetainTaskState = true
    , LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
    , ScreenOrientation = ScreenOrientation.Portrait
    , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
    View pView = null;
    Game1 game = null;
    IAsyncResult ar = null;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        game = new Game1();

        pView = (View)game.Services.GetService(typeof(View));
        SetContentView(pView);

        game.Run();
    }

    public void ShowKeyboard()
    {
        TakeKeyEvents(true);
        InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
        inputMethodManager.ShowSoftInput(pView, ShowFlags.Forced);
        inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
    }

    public void HideKeyboard()
    {
        InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
        inputMethodManager.HideSoftInputFromWindow(pView.WindowToken, HideSoftInputFlags.None);
        TakeKeyEvents(false);
    }

    public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
    {
        return base.OnKeyUp(keyCode, e); // Never gets called
    }

    private void OnKeyPress(object sender, View.KeyEventArgs e)
    {
         // Never gets called
    }
}

第二种方法使用XNA自带的Guide.BeginShowKeyboardInput()方法。它显示一个带有键盘的文本框。当您点击接受按钮时,会调用一个 AsyncCallback。在该回调中,我试图弄清楚如何获取用户键入的文本,但不知道如何获取。我在这里找不到任何有关实际从软键盘获取价值的有用文档。

方法二实现

void KeyboardCallback(IAsyncResult Result)
{
    System.Runtime.Remoting.Messaging.AsyncResult res = Result as System.Runtime.Remoting.Messaging.AsyncResult;
}

void CreateButtonClicked(Panel panel)
{
    ar = Guide.BeginShowKeyboardInput(PlayerIndex.One, "Game1", "Enter a username", "", KeyboardCallback, obj);
}

ar 是由 show keyboard 方法返回的 IAsyncResult 对象。我试图查看 IAsyncResult 对象中的值以查找我在键盘中输入的文本,但我什么也没看到。

【问题讨论】:

    标签: c# android xamarin monogame on-screen-keyboard


    【解决方案1】:

    我知道我可能为时已晚,但这是你需要做的:

    void KeyboardCallback(IAsyncResult Result)
    {
        var text = Guide.EndKeyboardInput(result);
        //text now contains your keyboard input
    }
    
    void CreateButtonClicked(Panel panel)
    {
        ar = Guide.BeginShowKeyboardInput(PlayerIndex.One, "Game1", "Enter a username", "", KeyboardCallback, null);
    }
    

    希望对您有所帮助...

    【讨论】:

      【解决方案2】:

      我厌倦了这种键盘混乱,我创建了自己的键盘类,其中包含每个字母的简单按钮。 当文本输入对象变为活动状态等时,我会显示它。

      可能不是最好的解决方案,但至少它适用于 android 和 windows。

      【讨论】:

        猜你喜欢
        • 2011-07-24
        • 2015-10-17
        • 2014-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 2012-07-21
        相关资源
        最近更新 更多