Android键盘暂停

时间:2012-05-02 10:39:53

标签: java android triggers keyboard onkeydown

我一直试图在键盘的输入键上创建一个“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),都有默认的软键盘

1 个答案:

答案 0 :(得分:0)

onKeyLongPress event

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

所以你的代码应该像

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

活动子类 KeyEvent.Callback 开始,您可以直接从那里调用 AskForDate()