如何使keyevent仅在有文本时执行单击

时间:2017-02-26 15:01:03

标签: java android actionlistener keyevent

所以我试图设置一个具有扫描按钮的新设备的关键事件,我之前使用虚拟键盘让它工作但是我无法使用硬件键

在版本4.1上使用虚拟键盘在另一台设备上工作,但也无法在5.1的新设备上工作

assertEqual(a, b)

我想要开始工作

shelfnumberbox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_NULL
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            btn_aprove.performClick();
        }
        return false;

    }
});
}

api的全功能硬件keyevent

shelfnumberbox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_NULL
                    && event.getAction() == KeyEvent.KEYCODE_BUTTON_A) {
                btn_aprove.performClick();
            }
            return false;

        }
    });

}

1 个答案:

答案 0 :(得分:0)

在执行点击事件之前,只需检查是否有文字:

if (v.getText().toString().length() > 0) {
    btn_aprove.performClick();
}

因此:

    if (actionId == EditorInfo.IME_NULL
            && event.getAction() == KeyEvent.ACTION_DOWN) {
        if (v.getText().toString().length() > 0) {
            btn_aprove.performClick();
        }
    }