【发布时间】: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