我调试了以下代码段,并了解了调用方法dispatchKeyEvent()
两次。请提出解决方案
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode()== KeyEvent.KEYCODE_DPAD_CENTER)
{
if(cDetail.getVisibility()==View.VISIBLE )
{
cDetail.setVisibility(View.INVISIBLE);
cList.setVisibility(View.INVISIBLE);
}
else
{
cDetail.setVisibility(View.VISIBLE);
cList.setVisibility(View.VISIBLE);
}
}
return super.dispatchKeyEvent(event);
}
答案 0 :(得分:3)
dispatchKeyEvent fire两次:第一次按下键,第二次按键,所以你必须过滤:
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return true;
switch (keyCode) {
case KeyEvent.KEYCODE_1 :
MakeToast(1);
break;
case KeyEvent.KEYCODE_2 :
MakeToast(2);
break;
case KeyEvent.KEYCODE_3 :
MakeToast(3);
break;
}