Android onKeyListener问题

时间:2013-11-16 08:12:30

标签: android

我有一个editText,我希望当用户按下键盘上的空格键时将其清空。 (这里是android的新手=])

 UserInput.setOnKeyListener(new EditText.OnKeyListener(){
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if((event.getAction() == KeyEvent.ACTION_DOWN) && (event.getKeyCode() == event.KEYCODE_SPACE)) {
                UserInput.setText("");
            }
            return false;
        }
    });

任何帮助将不胜感激! =)

1 个答案:

答案 0 :(得分:0)

当用户单击空格按钮定义您自己的操作时,尝试这对您肯定有用...

youredittext.setOnKeyListener(new View.OnKeyListener() {
      public boolean onKey(View v,  int keyCode, KeyEvent event) {

        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_SPACE)) {
          // Perform action on key press
           youredittext.setText("");
          Toast.makeText(getApplicationContext(), youredittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});