在 Monogame 中接收来自 Android 软键盘的输入
Receiving input from Android soft keyboard in Monogame
我只是想接收用户在使用 Android 的系统键盘时按下的字符。 OSK 显示正常,但我无法接收用户输入的字符。我尝试了两种不同的方法(我能找到的仅有的两种)。
第一种方法是在 Activity
class 中使用 View
对象,如 所示。我无法让它工作,所以我一直在搜索并尝试不同的东西,但没有任何效果。
我的Activity
class
[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
是显示键盘方法返回的 IAsyncResult
对象。我试图查看 IAsyncResult 对象中的值以查找我在键盘中输入的文本,但我没有看到任何内容。
我受够了这种乱七八糟的键盘,所以我创建了自己的键盘 class,每个字母都包含简单的按钮。
我在 textinput-object 激活时显示它等
可能不是最好的解决方案,但至少它适用于 android 和 windows。
我知道我可能来不及了,但这是你需要做的:
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);
}
希望对您有所帮助...
我只是想接收用户在使用 Android 的系统键盘时按下的字符。 OSK 显示正常,但我无法接收用户输入的字符。我尝试了两种不同的方法(我能找到的仅有的两种)。
第一种方法是在 Activity
class 中使用 View
对象,如
我的Activity
class
[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
是显示键盘方法返回的 IAsyncResult
对象。我试图查看 IAsyncResult 对象中的值以查找我在键盘中输入的文本,但我没有看到任何内容。
我受够了这种乱七八糟的键盘,所以我创建了自己的键盘 class,每个字母都包含简单的按钮。 我在 textinput-object 激活时显示它等
可能不是最好的解决方案,但至少它适用于 android 和 windows。
我知道我可能来不及了,但这是你需要做的:
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);
}
希望对您有所帮助...