我是菜鳥失尖,在android TV開發(fā)中遇到問題,記錄下來渐苏,監(jiān)聽android 中的長按事件一般要執(zhí)行 onKeyDown掀潮,onKeyLongPress,onKeyUp這三個方法琼富。
? private static double DOUBLE_CLICK_TIME = 0L;
? private boolean isLongPressKey ;
? private boolean lockLongPressKey;//判斷長按還是
? private boolean isDoublePressKey;//判斷是否快速點擊
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_DOWN:
if (event.getRepeatCount() == 0) {
event.startTracking();
isLongPressKey=false;
if((System.currentTimeMillis() - DOUBLE_CLICK_TIME)>600){
isDoublePressKey=false;
}else{
isDoublePressKey=true;
}
DOUBLE_CLICK_TIME = System.currentTimeMillis();
return true;
}else{
isLongPressKey=true;
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_DOWN:
lockLongPressKey = true;
return true;
}
return super.onKeyDown(keyCode, event);
}
int mKeyTimes=0;//用來按下的上下鍵長按執(zhí)行的次數(shù)
int mKeyRunTimes=0;
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && isDoublePressKey){
mKeyTimes++;
}
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_DOWN:
if(lockLongPressKey){
Log.d("xiaowu","isLongPressKey,444 "+event.getRepeatCount()+ "=="+isLongPressKey);
// TODO: 2017/6/11 加載數(shù)據(jù)
isLongPressKey=false;
lockLongPressKey=false;
}else{
if(!isLongPressKey ){
if(isDoublePressKey){
//任務(wù)延時加載
}else{
// TODO: 2017/6/11 加載數(shù)據(jù)
}
}
Log.d("xiaowu","isLongPressKey,333 "+event.getRepeatCount()+ "=="+isLongPressKey);
}
return true;
}
return super.onKeyUp(keyCode, event);
}