c# –Xamarin自定义键盘

c# –Xamarin自定义键盘,第1张

概述我正在尝试为特定页面创建自定义键盘,并且在处理所有键的侦听器时遇到一些问题,并且此文档非常有限.我正在使用Xamarin和C#进行开发:所以我在这里有一个活动(OrderActivity):mKeyboard=newKeyboard(this,Resource.Layout.Keyboard);mKeyboardView=this.FindViewById<Keyboa

我正在尝试为特定页面创建自定义键盘,并且在处理所有键的侦听器时遇到一些问题,并且此文档非常有限.

我正在使用Xamarin和C#进行开发:

所以我在这里有一个活动(OrderActivity):

mKeyboard = new Keyboard(this,Resource.Layout.Keyboard);mKeyboardVIEw = this.FindVIEwByID<KeyboardVIEw> (Resource.ID.keyboardvIEw);mKeyboardVIEw.Keyboard = mKeyboard;// PROBLEM HEREmKeyboardVIEw.OnKeyboardActionListener = new KeyboardVIEw.IOnKeyboardActionListener () {};

有一个Keyboard.axml,它们完美地显示在屏幕上,但我有问题,不知道如何调用监听器,这里有人有任何教程或如何解决这个问题?或者在创建自定义键盘时有其他选择吗?

解决方法:

正如侦听器的名称所示,它是您需要实现的接口. 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.

总结

以上是内存溢出为你收集整理的c# – Xamarin自定义键盘全部内容,希望文章能够帮你解决c# – Xamarin自定义键盘所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:http://outofmemory.cn/web/1114976.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存