Xamarin 自定义键盘
Xamarin Custom Keyboard
我正在尝试为特定页面创建一个 custom keyboard
,但我在处理所有键的侦听器时遇到了一些问题,而且这方面的文档非常有限。
我正在使用 Xamarin 和 C# 进行开发:
所以这里我有一个 activity (OrderActivity):
mKeyboard = new Keyboard(this,Resource.Layout.Keyboard);
mKeyboardView = this.FindViewById<KeyboardView> (Resource.Id.keyboardview);
mKeyboardView.Keyboard = mKeyboard;
// PROBLEM HERE
mKeyboardView.OnKeyboardActionListener = new KeyboardView.IOnKeyboardActionListener () {};
有一个 Keyboard.axml,它们在屏幕上显示得很好,但我有问题,不知道如何调用监听器,这里有人有任何教程或如何解决这个问题吗?或者是否有创建自定义键盘的替代方法?
正如监听器的名称所示,它是您需要实现的 Interface
。 C# 目前不允许匿名 类 就像 Java 一样。
请记住,在实现 Java 接口时,您需要从 Java.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<KeyboardOnKeyEventArgs> Key;
public event EventHandler<KeyboardKeyCodeEventArgs> Press;
public event EventHandler<KeyboardKeyCodeEventArgs> Release;
public event EventHandler<KeyboardOnTextEventArgs> 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
<?xml version="1.0" encoding="utf-8" ?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="54dip">
<Row>
<Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="9" android:keyLabel="2" />
<Key android:codes="10" android:keyLabel="3" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="12" android:keyLabel="5" />
<Key android:codes="13" android:keyLabel="6" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="14" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="15" android:keyLabel="8" />
<Key android:codes="16" android:keyLabel="9" android:keyEdgeFlags="right" />
</Row>
</Keyboard>
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_above="@+id/keyboard_view"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:id="@+id/target"
android:layout_height="wrap_content" />
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard_view"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
slide_up.xml
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-50%p" android:toYDelta="0" android:duration="200"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="200" />
</set>
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<KeyboardView>(Resource.Id.keyboard_view);
_keyboardView.Keyboard = _keyBoard;
_keyboardView.OnKeyboardActionListener = new MyKeyboardListener(this);
_targetView = FindViewById<EditText>(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)
我正在尝试为特定页面创建一个 custom keyboard
,但我在处理所有键的侦听器时遇到了一些问题,而且这方面的文档非常有限。
我正在使用 Xamarin 和 C# 进行开发:
所以这里我有一个 activity (OrderActivity):
mKeyboard = new Keyboard(this,Resource.Layout.Keyboard);
mKeyboardView = this.FindViewById<KeyboardView> (Resource.Id.keyboardview);
mKeyboardView.Keyboard = mKeyboard;
// PROBLEM HERE
mKeyboardView.OnKeyboardActionListener = new KeyboardView.IOnKeyboardActionListener () {};
有一个 Keyboard.axml,它们在屏幕上显示得很好,但我有问题,不知道如何调用监听器,这里有人有任何教程或如何解决这个问题吗?或者是否有创建自定义键盘的替代方法?
正如监听器的名称所示,它是您需要实现的 Interface
。 C# 目前不允许匿名 类 就像 Java 一样。
请记住,在实现 Java 接口时,您需要从 Java.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<KeyboardOnKeyEventArgs> Key;
public event EventHandler<KeyboardKeyCodeEventArgs> Press;
public event EventHandler<KeyboardKeyCodeEventArgs> Release;
public event EventHandler<KeyboardOnTextEventArgs> 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
<?xml version="1.0" encoding="utf-8" ?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="54dip">
<Row>
<Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="9" android:keyLabel="2" />
<Key android:codes="10" android:keyLabel="3" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="12" android:keyLabel="5" />
<Key android:codes="13" android:keyLabel="6" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="14" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="15" android:keyLabel="8" />
<Key android:codes="16" android:keyLabel="9" android:keyEdgeFlags="right" />
</Row>
</Keyboard>
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_above="@+id/keyboard_view"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:id="@+id/target"
android:layout_height="wrap_content" />
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard_view"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
slide_up.xml
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-50%p" android:toYDelta="0" android:duration="200"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="200" />
</set>
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<KeyboardView>(Resource.Id.keyboard_view);
_keyboardView.Keyboard = _keyBoard;
_keyboardView.OnKeyboardActionListener = new MyKeyboardListener(this);
_targetView = FindViewById<EditText>(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)