Android系统中自定义按键的短按、双击、长按事件
生活随笔
收集整理的這篇文章主要介紹了
Android系统中自定义按键的短按、双击、长按事件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在項(xiàng)目中碰到這樣的問(wèn)題:
由于系統(tǒng)中的按鍵在底層做了重新定義或者新增了按鍵,此時(shí)需要在APP層對(duì)按鍵事件(keyevent)做分解處理,模擬Android系統(tǒng)做法,把keyevent分解成:
1、單擊事件:就是普通key的單擊;
2、雙擊事件:500ms內(nèi)同一按鍵單擊兩次;
3、長(zhǎng)按事件:同一按鍵長(zhǎng)按超過(guò)1000ms(系統(tǒng)中長(zhǎng)按事件為500ms);
4、組合按鍵:兩個(gè)以上按鍵同時(shí)按住;
其中的keyevent可以來(lái)自Activity、View子類的dispatchKeyEvent方法,也可以是我們自定義的接口,也可以是我們發(fā)廣播送上來(lái)的,根據(jù)項(xiàng)目需求;
關(guān)于各事件的原理:
1、雙擊事件:每次點(diǎn)擊的up事件中啟動(dòng)一個(gè)定時(shí)(500ms)線程消息,用Handler.postDelayed()方法。
2、長(zhǎng)按事件:每次點(diǎn)擊的down事件中啟動(dòng)一個(gè)定時(shí)(1000ms)線程消息,用Handler.postDelayed()方法,注意:在RepeatCount==0時(shí)啟動(dòng);
3、組合按鍵:用變量記錄每個(gè)按鍵的狀態(tài),再進(jìn)行判斷;
具體代碼如下:
Java代碼 ?package?com.jerome.util;?? ?? import?android.content.Context;?? import?android.os.Handler;?? import?android.util.Log;?? import?android.view.KeyEvent;?? ?? public?class?KeyUtil?{?? ????private?boolean?isVolumeDown?=?false;?? ????private?boolean?isVolumeUp?=?false;?? ????private?boolean?isMenu?=?false;?? ????private?int?currentKeyCode?=?0;?? ?? ????private?static?Boolean?isDoubleClick?=?false;?? ????private?static?Boolean?isLongClick?=?false;?? ?? ????CheckForLongPress?mPendingCheckForLongPress?=?null;?? ????CheckForDoublePress?mPendingCheckForDoublePress?=?null;?? ????Handler?mHandler?=?new?Handler();?? ?? ????Context?mContext?=?null;?? ????private?String?TAG?=?"";?? ?? ????public?KeyUtil(Context?context,?String?tag)?{?? ????????mContext?=?context;?? ????????TAG?=?tag;?? ????}?? ?? ????public?void?dispatchKeyEvent(KeyEvent?event)?{?? ????????int?keycode?=?event.getKeyCode();?? ?? ????????//?有不同按鍵按下,取消長(zhǎng)按、短按的判斷?? ????????if?(currentKeyCode?!=?keycode)?{?? ????????????removeLongPressCallback();?? ????????????isDoubleClick?=?false;?? ????????}?? ?? ????????//?處理長(zhǎng)按、單擊、雙擊按鍵?? ????????if?(event.getAction()?==?KeyEvent.ACTION_DOWN)?{?? ????????????checkForLongClick(event);?? ????????}?else?if?(event.getAction()?==?KeyEvent.ACTION_UP)?{?? ????????????checkForDoubleClick(event);?? ????????}?? ?? ????????if?(keycode?==?KeyEvent.KEYCODE_VOLUME_DOWN)?{?? ????????????if?(event.getAction()?==?KeyEvent.ACTION_DOWN)?{?? ????????????????isVolumeDown?=?true;?? ????????????}?else?if?(event.getAction()?==?KeyEvent.ACTION_UP)?{?? ????????????????isVolumeDown?=?false;?? ????????????}?? ????????}?else?if?(keycode?==?KeyEvent.KEYCODE_VOLUME_UP)?{?? ????????????if?(event.getAction()?==?KeyEvent.ACTION_DOWN)?{?? ????????????????isVolumeUp?=?true;?? ????????????}?else?if?(event.getAction()?==?KeyEvent.ACTION_UP)?{?? ????????????????isVolumeUp?=?false;?? ????????????}?? ????????}?else?if?(keycode?==?KeyEvent.KEYCODE_MENU)?{?? ????????????if?(event.getAction()?==?KeyEvent.ACTION_DOWN)?{?? ????????????????isMenu?=?true;?? ????????????}?else?if?(event.getAction()?==?KeyEvent.ACTION_UP)?{?? ????????????????isMenu?=?true;?? ????????????}?? ????????}?? ?? ????????//?判斷組合按鍵?? ????????if?(isVolumeDown?? ????????????????&&?isVolumeUp?? ????????????????&&?isMenu?? ????????????????&&?(keycode?==?KeyEvent.KEYCODE_VOLUME_UP?? ????????????????????????||?keycode?==?KeyEvent.KEYCODE_VOLUME_DOWN?||?keycode?==?KeyEvent.KEYCODE_MENU)?? ????????????????&&?event.getAction()?==?KeyEvent.ACTION_DOWN)?{?? ????????????//組合按鍵事件處理;?? ????????????isVolumeDown?=?false;?? ????????????isVolumeUp?=?false;?? ????????????isMenu?=?false;?? ????????}?? ????}?? ?? ????private?void?removeLongPressCallback()?{?? ????????if?(mPendingCheckForLongPress?!=?null)?{?? ????????????mHandler.removeCallbacks(mPendingCheckForLongPress);?? ????????}?? ????}?? ?? ????private?void?checkForLongClick(KeyEvent?event)?{?? ????????int?count?=?event.getRepeatCount();?? ????????int?keycode?=?event.getKeyCode();?? ????????if?(count?==?0)?{?? ????????????currentKeyCode?=?keycode;?? ????????}?else?{?? ????????????return;?? ????????}?? ????????if?(mPendingCheckForLongPress?==?null)?{?? ????????????mPendingCheckForLongPress?=?new?CheckForLongPress();?? ????????}?? ????????mPendingCheckForLongPress.setKeycode(event.getKeyCode());?? ????????mHandler.postDelayed(mPendingCheckForLongPress,?1000);?? ????}?? ?? ????class?CheckForLongPress?implements?Runnable?{?? ?? ????????int?currentKeycode?=?0;?? ?? ????????public?void?run()?{?? ????????????isLongClick?=?true;?? ????????????longPress(currentKeycode);?? ????????}?? ?? ????????public?void?setKeycode(int?keycode)?{?? ????????????currentKeycode?=?keycode;?? ????????}?? ????}?? ?? ????private?void?longPress(int?keycode)?{?? ????????Log.i(TAG,?"--longPress?長(zhǎng)按事件--"?+?keycode);?? ????}?? ?? ????private?void?singleClick(int?keycode)?{?? ????????Log.i(TAG,?"--singleClick?單擊事件--"?+?keycode);?? ????}?? ?? ????private?void?doublePress(int?keycode)?{?? ????????Log.i(TAG,?"---doublePress?雙擊事件--"?+?keycode);?? ????}?? ?? ????private?void?checkForDoubleClick(KeyEvent?event)?{?? ????????//?有長(zhǎng)按時(shí)間發(fā)生,則不處理單擊、雙擊事件?? ????????removeLongPressCallback();?? ????????if?(isLongClick)?{?? ????????????isLongClick?=?false;?? ????????????return;?? ????????}?? ?? ????????if?(!isDoubleClick)?{?? ????????????isDoubleClick?=?true;?? ????????????if?(mPendingCheckForDoublePress?==?null)?{?? ????????????????mPendingCheckForDoublePress?=?new?CheckForDoublePress();?? ????????????}?? ????????????mPendingCheckForDoublePress.setKeycode(event.getKeyCode());?? ????????????mHandler.postDelayed(mPendingCheckForDoublePress,?500);?? ????????}?else?{?? ????????????//?500ms內(nèi)兩次單擊,觸發(fā)雙擊?? ????????????isDoubleClick?=?false;?? ????????????doublePress(event.getKeyCode());?? ????????}?? ????}?? ?? ????class?CheckForDoublePress?implements?Runnable?{?? ?? ????????int?currentKeycode?=?0;?? ?? ????????public?void?run()?{?? ????????????if?(isDoubleClick)?{?? ????????????????singleClick(currentKeycode);?? ????????????}?? ????????????isDoubleClick?=?false;?? ????????}?? ?? ????????public?void?setKeycode(int?keycode)?{?? ????????????currentKeycode?=?keycode;?? ????????}?? ????}?? ?? ????private?void?removeDoublePressCallback()?{?? ????????if?(mPendingCheckForDoublePress?!=?null)?{?? ????????????mHandler.removeCallbacks(mPendingCheckForDoublePress);?? ????????}?? ????}?? }??
注意:
只有Action Down狀態(tài)下RepeatCount才會(huì)>0,避免長(zhǎng)按和單擊事件混淆;
由于系統(tǒng)中的按鍵在底層做了重新定義或者新增了按鍵,此時(shí)需要在APP層對(duì)按鍵事件(keyevent)做分解處理,模擬Android系統(tǒng)做法,把keyevent分解成:
1、單擊事件:就是普通key的單擊;
2、雙擊事件:500ms內(nèi)同一按鍵單擊兩次;
3、長(zhǎng)按事件:同一按鍵長(zhǎng)按超過(guò)1000ms(系統(tǒng)中長(zhǎng)按事件為500ms);
4、組合按鍵:兩個(gè)以上按鍵同時(shí)按住;
其中的keyevent可以來(lái)自Activity、View子類的dispatchKeyEvent方法,也可以是我們自定義的接口,也可以是我們發(fā)廣播送上來(lái)的,根據(jù)項(xiàng)目需求;
關(guān)于各事件的原理:
1、雙擊事件:每次點(diǎn)擊的up事件中啟動(dòng)一個(gè)定時(shí)(500ms)線程消息,用Handler.postDelayed()方法。
2、長(zhǎng)按事件:每次點(diǎn)擊的down事件中啟動(dòng)一個(gè)定時(shí)(1000ms)線程消息,用Handler.postDelayed()方法,注意:在RepeatCount==0時(shí)啟動(dòng);
3、組合按鍵:用變量記錄每個(gè)按鍵的狀態(tài),再進(jìn)行判斷;
具體代碼如下:
Java代碼 ?
注意:
只有Action Down狀態(tài)下RepeatCount才會(huì)>0,避免長(zhǎng)按和單擊事件混淆;
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/6113297.html
總結(jié)
以上是生活随笔為你收集整理的Android系统中自定义按键的短按、双击、长按事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python-类知识点简介
- 下一篇: 【iOS atomic、nonatomi