ProgressDialog在Android中的关键监听器的奇怪行为

时间:2012-06-08 09:57:06

标签: android

我是一个小程序来测试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

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

1 个答案:

答案 0 :(得分:0)

onKeyKeyEvent.ACTION_UP执行

KeyEvent.ACTION_DOWN。 (即arg2

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