0

我一直在尝试在键盘的 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) 上测试,均使用默认软键盘

4

1 回答 1

0

onKeyLongPress 事件

试试上面那个,它说你必须在你的监听器中调用startTracking

所以你的代码应该像

if((event.getAction() == KeyEvent.ACTION_DOWN  ) && (keyCode  == KeyEvent.KEYCODE_ENTER))
{
   event.startTracking();
}

并且由于Activity是KeyEvent.Callback的子类,因此您可以从那里直接调用您的AskForDate()

于 2012-05-02T17:11:58.183 回答