点击确认键之后执行两次的情况:
private View.OnKeyListener onKeyListsner = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//do somthing
Toast.makeText(currentContext, "执行两次操作", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
加上 event.getAction() == KeyEvent.ACTION_DOWN 条件之后就可以解决执行两次的问题
private View.OnKeyListener onKeyListsner = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//do somthing
Toast.makeText(currentContext, "执行一次操作", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
Android软键盘确认键执行两次
点赞
收藏