Soft keyboard enter key event handle
I want to do some stuff on soft keyboard enter key press. See my code and output below. It executes twice, 开发者_开发百科but I want it to be executed only once. How to fix it?
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;
}
});
}
The best way possible:
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
Log.d(TAG, "enter_key_called");
}
精彩评论