我一直在尝试在键盘的 Enter 键上创建一个“onHold”操作。
searchField.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
searchField.setText(searchField.getText().toString().replace("\n", ""));
if((CounterRunning)&&(event.getAction() == KeyEvent.ACTION_UP ) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
CounterRunning = false;
counter.cancel();
AddItem();
}
if((event.getAction() == KeyEvent.ACTION_DOWN ) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
CounterRunning = true;
counter.start();
}
}});
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
CounterRunning = false;
AskForDate();
}
@Override
public void onTick(long millisUntilFinished) {
}
}
如果用户按住回车键,我希望用户AddItem()
在单击时运行并运行。AskForDate()
但是KeyEvent.ACTION_DOWN
只有当我从键盘上移开手指时才会触发该动作,我错过了什么吗?
在 android 2.3.7 (CM7.2) 和 android 4.0.4 (CM9) 上测试,均使用默认软键盘