我有一个简单的应用程序,我执行以下操作:
public void onClick(View v){
switch(v.getId()) {
case R.id.buttonup:
onButtonUp();
break;
case R.id.buttondown:
onButtonDown();
break;
}
}
public boolean dispatchKeyEvent(KeyEvent event){
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
onButtonUp();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
onButtonDown();
return true;
default:
return super.dispatchKeyEvent(event);
}
}
void onButtonUp(){
increment_some_static_class_variable;
}
void onButtonUp(){
decrement_some_static_class_variable;
}
Run Code Online (Sandbox Code Playgroud)
问题是,每当我按下音量按钮时,onButtonUp和onButtondown函数都会被调用两次.当我按下屏幕按钮(在onClick(View)中处理)时,不会发生这种情况.我没有找到任何人有这个问题,所以我问这里的人.我是Android新手,这是我的第一个应用程序.使用Log,我发现对onButtonUp和onButtonDown的调用都来自dispatchKeyEvent函数.这可能有什么问题?我希望我能很好地解释这个问题.建议/解决方案是最受欢迎的.