android 长按,快速按的按键处理

我是菜鸟,在android TV开发中遇到问题,记录下来,监听android 中的长按事件一般要执行 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;//用来按下的上下键长按执行的次数

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 加载数据

isLongPressKey=false;

lockLongPressKey=false;

}else{

if(!isLongPressKey ){

if(isDoublePressKey){

//任务延时加载

}else{

// TODO: 2017/6/11 加载数据

}

}

Log.d("xiaowu","isLongPressKey,333 "+event.getRepeatCount()+ "=="+isLongPressKey);

}

return true;

}

return super.onKeyUp(keyCode, event);

}

你可能感兴趣的:(android 长按,快速按的按键处理)