2012-06-08 2 views
0

Я беру небольшую программу для тестирования ProgressDialog на Android.Странное поведение главного слушателя ProgressDialog в Android

DialogInterface.OnKeyListener dialogKey = new DialogInterface.OnKeyListener() { 
     public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) { 
      switch(arg1) { 
      case KeyEvent.KEYCODE_VOLUME_DOWN: 
       Log.i("Spinner Key", "Key Volume Down"); 
          Log.i("Spinner Key", "" + arg0.toString()); 
       return true; 
      case KeyEvent.KEYCODE_VOLUME_UP:  
       Log.i("Spinner Key", "Key Volume Up"); 
          Log.i("Spinner Key", "" + arg0.toString()); 
       return true;     
      default: 
        break; 
      } 
      return true; 
     } 
    };    


     this.mypDialog = new ProgressDialog(SensorDefenceActivity.this);    
     this.mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     this.mypDialog.setTitle("Count"); 
     this.mypDialog.setIndeterminate(false); 
     this.mypDialog.setMessage(this.start + ""); 
     this.mypDialog.setCancelable(false); 
     this.mypDialog.setCanceledOnTouchOutside(false); 
     this.mypDialog.setButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int i) 
      { 
       dialog.cancel(); 
      } 
     }); 

     mypDialog.setOnKeyListener(dialogKey); 
     mypDialog.show(); 
     mypDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); 

Когда появится диалоговое окно, я нажимаю одну из клавиш регулировки громкости. Но я получаю одно и то же ключевое уведомление о прессе в logcat дважды!

06-08 17:54:07.130: I/Spinner Key(11636): Key Volume Up 
06-08 17:54:07.130: I/Spinner Key(11636): [email protected] 
06-08 17:54:07.260: I/Spinner Key(11636): Key Volume Up 
06-08 17:54:07.260: I/Spinner Key(11636): [email protected] 
06-08 17:54:11.320: I/Spinner Key(11636): Key Volume Down 
06-08 17:54:11.320: I/Spinner Key(11636): [email protected] 
06-08 17:54:11.530: I/Spinner Key(11636): Key Volume Down 
06-08 17:54:11.530: I/Spinner Key(11636): [email protected] 

Я обещаю, что время печати очень короткое, чтобы он не мог вызвать одно и то же сообщение с нажатием клавиши дважды.

ответ

0

onKey выполнен для обоих KeyEvent.ACTION_UP и KeyEvent.ACTION_DOWN. (т.е. arg2)

Выполните свой код только для одного из них. Думаю, ACTION_DOWN должен быть единственным.

Смежные вопросы