软键盘输入键事件句柄

时间:2011-09-30 13:30:27

标签: android

我想在软键盘上做一些东西输入按键。请参阅下面的代码和输出。它执行两次,但我希望它只执行一次。如何解决?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt = (EditText)findViewById(R.id.txt);
    txt.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                Log.e("test","--------- on enter");
                return false;
            }
            return false;
        }
    });
}

This is my output for this

1 个答案:

答案 0 :(得分:20)

最好的方法:

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
    Log.d(TAG, "enter_key_called");
}