Xamarin自定义键盘
我正在尝试为特定页面创建custom keyboard
,并且在处理所有键的侦听器时遇到一些问题,并且此文档非常有限。
我正在使用Xamarin和C#进行开发:
所以我在这里有一个活动(OrderActivity):
mKeyboard = new Keyboard(this,Resource.Layout.Keyboard); mKeyboardView = this.FindViewById (Resource.Id.keyboardview); mKeyboardView.Keyboard = mKeyboard; // PROBLEM HERE mKeyboardView.OnKeyboardActionListener = new KeyboardView.IOnKeyboardActionListener () {};
有一个Keyboard.axml,它们完美地显示在屏幕上,但我有问题,不知道如何调用监听器,这里有人有任何教程或如何解决这个问题? 或者在创建自定义键盘时有其他选择吗?
正如侦听器的名称所示,它是您需要实现的Interface
。 C#目前不像Java那样允许匿名类。
请记住,在实现Java接口时,您需要inheritanceJava.Lang.Object
,因为它需要一个句柄。
所以你的实现可能看起来像:
public class KeyboardOnKeyEventArgs : KeyboardKeyCodeEventArgs { public Keycode[] KeyCodes { get; set; } } public class KeyboardKeyCodeEventArgs : EventArgs { public Keycode PrimaryCode { get; set; } } public class KeyboardOnTextEventArgs : EventArgs { public ICharSequence Text { get; set; } } public class MyKeyboardListener : Java.Lang.Object, KeyboardView.IOnKeyboardActionListener { public event EventHandler Key; public event EventHandler Press; public event EventHandler Release; public event EventHandler Text; public event EventHandler OnSwipeDown; public event EventHandler OnSwipeLeft; public event EventHandler OnSwipeRight; public event EventHandler OnSwipeUp; public void OnKey(Keycode primaryCode, Keycode[] keyCodes) { if (Key != null) Key(this, new KeyboardOnKeyEventArgs { KeyCodes = keyCodes, PrimaryCode = primaryCode }); } public void OnPress(Keycode primaryCode) { if (Press != null) Press(this, new KeyboardKeyCodeEventArgs { PrimaryCode = primaryCode }); } public void OnRelease(Keycode primaryCode) { if (Release != null) Release(this, new KeyboardKeyCodeEventArgs { PrimaryCode = primaryCode }); } public void OnText(ICharSequence text) { if (Text != null) Text(this, new KeyboardOnTextEventArgs {Text = text}); } public void SwipeDown() { if(OnSwipeDown != null) OnSwipeDown(this, EventArgs.Empty); } public void SwipeLeft() { if (OnSwipeLeft != null) OnSwipeLeft(this, EventArgs.Empty); } public void SwipeRight() { if (OnSwipeRight != null) OnSwipeRight(this, EventArgs.Empty); } public void SwipeUp() { if (OnSwipeUp != null) OnSwipeUp(this, EventArgs.Empty); } }
用法是:
var keyboardListener = new MyKeyboardListener(); keyboardListener.Press += (s, e) => { /* do something */ }; mKeyboardView.OnKeyboardActionListener = keyboardListener;
编辑:
用它工作了一点,我无法重现你的错误。
keyboard.xml
Main.axml
slide_up.xml
MyKeyboardListener.cs
public class MyKeyboardListener : Java.Lang.Object, KeyboardView.IOnKeyboardActionListener { private readonly Activity _activity; public MyKeyboardListener(Activity activity) { _activity = activity; } public void OnKey(Keycode primaryCode, Keycode[] keyCodes) { var eventTime = DateTime.Now.Ticks; var keyEvent = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, primaryCode, 0); _activity.DispatchKeyEvent(keyEvent); } public void OnPress(Keycode primaryCode) { } public void OnRelease(Keycode primaryCode) { } public void OnText(ICharSequence text) { } public void SwipeDown() { } public void SwipeLeft() { } public void SwipeRight() { } public void SwipeUp() { } }
MainActivity.cs
[Activity(Label = "App14", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { private Keyboard _keyBoard; private EditText _targetView; private KeyboardView _keyboardView; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); _keyBoard = new Keyboard(this, Resource.Xml.keyboard); _keyboardView = FindViewById(Resource.Id.keyboard_view); _keyboardView.Keyboard = _keyBoard; _keyboardView.OnKeyboardActionListener = new MyKeyboardListener(this); _targetView = FindViewById(Resource.Id.target); _targetView.Touch += (sender, args) => { var bottomUp = AnimationUtils.LoadAnimation(this, Resource.Animation.slide_up); _keyboardView.StartAnimation(bottomUp); _keyboardView.Visibility = ViewStates.Visible; args.Handled = true; }; } }
键盘节目和字符被分派到EditText
。
关于如何隐藏软键盘弹出添加到您的Activity属性:
WindowSoftInputMode = Android.Views.SoftInput.StateAlwaysHidden
像这样:
Activity(MainLauncher = true, WindowSoftInputModeAndroid.Views.SoftInput.StateAlwaysHidden)
- 是否可以在C#中编写Android应用程序?
- ListView不会滚动分组
- MvvmCross; 如何从另一个ViewModel RaisePropertyChange
- 来自SQLite Database C#Android的Xamarin ListView显示项
- 使用C#从Xamarin Studio中的ListView获取EditText控件
- WCF Rest服务接收对象为null
- 为什么IAsyncResult将所有端口报告为已打开?
- 无法在Xamarin.Forms Visual Studio 2015中加载memtrack(无此类文件或目录)错误
- 如何从Uri xamarin android获取实际路径