如何覆盖键盘上的按键?

时间:2012-11-06 16:31:27

标签: android

  

可能重复:
  Catch keypress with android

当用户按下Enter按钮键盘时,我想执行一些操作。我怎么能得到这个动作?

2 个答案:

答案 0 :(得分:0)

使用以下代码:

public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
         /* This is a sample for handling the Enter button */
      return true;
    }
    return false;
}

并实施YourView.setOnKeyListener(this);

这是直接来自 this post.

答案 1 :(得分:0)

您也可以使用InputFilter:

InputFilter filter = new InputFilter()
{
    public CharSequence filter(CharSequence source, int start, int end,
    Spanned dest, int dstart, int dend)
    {
        for (int i = start; i < end; i++)
        {
            if (source.charAt(i) == '\n')
            {
                // Add whatever you want;
            }
        }
        return null;
    }
};