0

我写了一个小程序来测试Android中的ProgressDialog。

    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): android.app.ProgressDialog@402bdd78
06-08 17:54:07.260: I/Spinner Key(11636): Key Volume Up
06-08 17:54:07.260: I/Spinner Key(11636): android.app.ProgressDialog@402bdd78
06-08 17:54:11.320: I/Spinner Key(11636): Key Volume Down
06-08 17:54:11.320: I/Spinner Key(11636): android.app.ProgressDialog@402bdd78
06-08 17:54:11.530: I/Spinner Key(11636): Key Volume Down
06-08 17:54:11.530: I/Spinner Key(11636): android.app.ProgressDialog@402bdd78

我保证按下时间非常短,因此它不会触发相同的按键消息两次。

4

1 回答 1

0

onKey对 BothKeyEvent.ACTION_UP和执行KeyEvent.ACTION_DOWN。(即arg2

只为其中一个执行您的代码。我觉得ACTION_DOWN应该是那个。

于 2012-06-08T10:01:37.570 回答