您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Android開發(fā)中怎么實現(xiàn)一個長按Button鍵連續(xù)響應功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
預覽圖:
1.自定義Button.
/** * 長按連續(xù)響應的Button * Created by admin on 15-6-1. */ public class LongClickButton extends Button { /** * 長按連續(xù)響應的監(jiān)聽,長按時將會多次調(diào)用該接口中的方法直到長按結束 */ private LongClickRepeatListener repeatListener; /** * 間隔時間(ms) */ private long intervalTime; private MyHandler handler; public LongClickButton(Context context) { super(context); init(); } public LongClickButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LongClickButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化監(jiān)聽 */ private void init() { handler = new MyHandler(this); setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { new Thread(new LongClickThread()).start(); return true; } }); } /** * 長按時,該線程將會啟動 */ private class LongClickThread implements Runnable { private int num; @Override public void run() { while (LongClickButton.this.isPressed()) { num++; if (num % 5 == 0) { handler.sendEmptyMessage(1); } SystemClock.sleep(intervalTime / 5); } } } /** * 通過handler,使監(jiān)聽的事件響應在主線程中進行 */ private static class MyHandler extends Handler { private WeakReference<LongClickButton> ref; MyHandler(LongClickButton button) { ref = new WeakReference<>(button); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); LongClickButton button = ref.get(); if (button != null && button.repeatListener != null) { button.repeatListener.repeatAction(); } } } /** * 設置長按連續(xù)響應的監(jiān)聽和間隔時間,長按時將會多次調(diào)用該接口中的方法直到長按結束 * * @param listener 監(jiān)聽 * @param intervalTime 間隔時間(ms) */ public void setLongClickRepeatListener(LongClickRepeatListener listener, long intervalTime) { this.repeatListener = listener; this.intervalTime = intervalTime; } /** * 設置長按連續(xù)響應的監(jiān)聽(使用默認間隔時間100ms),長按時將會多次調(diào)用該接口中的方法直到長按結束 * * @param listener 監(jiān)聽 */ public void setLongClickRepeatListener(LongClickRepeatListener listener) { setLongClickRepeatListener(listener, 100); } public interface LongClickRepeatListener { void repeatAction(); } }
2.在Activity中調(diào)用:
LongClickButton buttonSub = (LongClickButton) findViewById(R.id.long_click_button1); LongClickButton buttonAdd = (LongClickButton) findViewById(R.id.long_click_button2); final TextView numberTV = (TextView) findViewById(R.id.main_number); //連續(xù)減 buttonSub.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() { @Override public void repeatAction() { numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) - 1)); } }, 50); //連續(xù)加 buttonAdd.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() { @Override public void repeatAction() { numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) + 1)); } }, 50); //減1 buttonSub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) - 1)); } }); //加1 buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) + 1)); } });
3.很多時候,長按的事件和普通點擊的事件是一樣的(也就是說,長按相當于連續(xù)的快速點擊)。這種情況下,自定義Button可以更加簡潔:即在長按時,連續(xù)的調(diào)用普通的OnClickListener。
public class LongClickButton2 extends Button { /** * 間隔時間(ms) */ private long intervalTime = 50; private MyHandler handler; public LongClickButton2(Context context) { super(context); init(); } public LongClickButton2(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LongClickButton2(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化監(jiān)聽 */ private void init() { handler = new MyHandler(this); setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { new Thread(new LongClickThread()).start(); return true; } }); } /** * 長按時,該線程將會啟動 */ private class LongClickThread implements Runnable { private int num; @Override public void run() { while (LongClickButton2.this.isPressed()) { num++; if (num % 5 == 0) { handler.sendEmptyMessage(1); } SystemClock.sleep(intervalTime / 5); } } } /** * 通過handler,使監(jiān)聽的事件響應在主線程中進行 */ private static class MyHandler extends Handler { private WeakReference<LongClickButton2> ref; MyHandler(LongClickButton2 button) { ref = new WeakReference<>(button); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); LongClickButton2 button = ref.get(); if (button != null) { //直接調(diào)用普通點擊事件 button.performClick(); } } } public void setIntervalTime(long intervalTime) { this.intervalTime = intervalTime; } }
看完上述內(nèi)容,你們對Android開發(fā)中怎么實現(xiàn)一個長按Button鍵連續(xù)響應功能有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。