在android Tv 上的開發(fā)時(shí),常常需要對(duì)按鍵的響應(yīng)速度做控制,否則容易出現(xiàn)焦點(diǎn)亂跳的情況或者出現(xiàn)來不及加載的情況。
這里給出一種方案:
在Activity或者view的dispatchKeyEvent做了處理。
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction() == 0) {
long nowTime = SystemClock.elapsedRealtime();
this.mTimeDelay = nowTime - this.mTimeLast;
this.mTimeLast = nowTime;
if(this.mTimeSpace <= 80L && this.mTimeDelay <= 80L) {
this.mTimeSpace += this.mTimeDelay;
return true;
}
this.mTimeSpace = 0L;
}
return super.dispatchKeyEvent(event);
}
這里的時(shí)間間隔是80ms,在兩次時(shí)間間隔小于80ms時(shí),將按鍵事件屏蔽,同時(shí)長(zhǎng)按產(chǎn)生的累加時(shí)間間隔要考慮。
還有一種方案是發(fā)送延遲handler消息。這個(gè)后面再補(bǔ)上