【问题标题】:Keypress on xamarin form not entryxamarin 表单上的按键不输入
【发布时间】:2019-10-16 10:05:45
【问题描述】:

我有一个 Xamarin Android 表单(内容页面),我希望在用户按下某个键盘键时执行某个操作,例如调用一个方法。我在任何地方都找不到任何解决方案。 如果有人知道如何做到这一点,我将非常感谢他/她的帮助。

谢谢

【问题讨论】:

  • Android 键盘上的 F1、F2 和 F3?我想你在这里混淆了一些东西。 Android 键盘没有这些按钮。
  • 我们正在使用一些在 Android 上运行的条码扫描器(终端)。

标签: android xamarin keypress


【解决方案1】:

尝试在您的MainActivity 中覆盖DispatchKeyEvent

    public override bool DispatchKeyEvent(KeyEvent e)
    {
        // if the Keycode is A .. there's lots of these
        //you'll have to checkout the Keycode class for more options
        //this example will fire the following code when 'A' is pressed
        if (e.KeyCode == Keycode.A)
        {
            //invoke your method here
        } 

        //return base if needed, otherwise keystrokes won't get automatically passed into textboxes etc and stuff will act wacky
        return base.DispatchKeyEvent(e);
    }

https://docs.microsoft.com/en-us/dotnet/api/android.app.activity.dispatchkeyevent?view=xamarin-android-sdk-9

如果这不起作用,您还可以覆盖 OnKeyDownOnKeyUpOnKey AFAIK

Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2017-04-23
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2019-02-07
    相关资源
    最近更新 更多