每次我按下输入键时,即使它不应该出来,吐司也会不断弹出......
在我的代码中花很多时间:
etAddMove.setOnKeyListener(new OnKeyListener() {
@SuppressLint("NewApi")
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getRepeatCount() == 0){
String ssmoveName = etAddMove.getText().toString();
int x = ssmoveName.length() - 1;
if (ssmoveName.isEmpty() || Character.isWhitespace(ssmoveName.charAt(0)) || Character.isWhitespace(ssmoveName.charAt(x))) {
Toast.makeText(ListMovingNames.this,
"Please enter a valid name! Avoid giving a blank name or white space at the beginning or end of the name",
Toast.LENGTH_LONG).show();
}else{
try {
SQLHandler check = new SQLHandler(ListMovingNames.this);
check.open();
String scheck = check.checkMove(ssmoveName);
check.close();
if (!scheck.equals(ssmoveName)) {
Toast.makeText(ListMovingNames.this, "Move name already exist please give a different name", Toast.LENGTH_LONG).show();
} else{
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
}
return true;
}
return onKeyDown(keyCode, event);
}
});
如果我不想表现出来,你怎么能让这家伙停下来……
编辑
我想我知道为什么会发生这种情况,这一定是因为每次我按 enter keyup 并且 keydown 都会初始化,这就是为什么第一次初始化是 keydown 调用条件并返回 false 并且当调用 keydown 时再次调用条件将返回真的,制作吐司展示。这就是我目前的想法......