【问题标题】:How to prevent a key to be inserted in an EditText if it comes from a specific keyboard如果键来自特定键盘,如何防止将键插入 EditText
【发布时间】:2013-04-15 14:26:46
【问题描述】:

我正在使用像蓝牙键盘一样的蓝牙条形码扫描仪,我想在我的用户扫描某些东西时处理按键,但我知道他可以将任何 EditText 集中在活动中,所以我想检测他使用的键盘我的 KeyEvent 是这样的:

if KeyEvent.getDevice().getName().equals("Datalogic Scanner") //do stuff
else //let the edittext add the letter

并防止编辑 EditText,而是存储和处理扫描仪发送的值。

我尝试在我的活动中的 dispatchKeyEvent 中返回 true, 我尝试在我的活动中在 onKeyDown 中返回 true, 我尝试在我的 EditText 上的 OnKeyListener 中返回 true,并在我的 EditText 类上覆盖 OnKeyDown,但没有任何效果,文本仍然插入到我的 EditText 中。

有什么想法吗?

【问题讨论】:

    标签: android barcode-scanner


    【解决方案1】:

    尝试像这样使用dispatchKeyEvent:

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return scanManager.handleDispatchKeyEvent(event);
    }
    

    在 scanManager 类中,您应该这样做:

    public boolean handleDispatchKeyEvent(KeyEvent event) {
    // handle the event
    return true;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-04
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多