本文实例讲述了Android编程实现长按Button按钮连续响应功能。分享给大家供大家参考,具体如下:
在电商类app的购物车页面,经常会有这样的需求:长按“+”按钮或者“-”按钮时,购物车中的商品数量连续的增加或减少。
本例的目的就是为了实现这个效果。预览图:
1.自定义Button.
public class LongClickButton extends Button { private LongClickRepeatListener repeatListener; 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(); } 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); } } } private static class MyHandler extends Handler { private WeakReferenceref; 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(); } } } public void setLongClickRepeatListener(LongClickRepeatListener listener, long intervalTime) { this.repeatListener = listener; this.intervalTime = intervalTime; } public void setLongClickRepeatListener(LongClickRepeatListener listener) { setLongClickRepeatListener(listener, 100); } public interface LongClickRepeatListener { void repeatAction(); } }
2.在Activity中调用:
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); //连续减 buttonSub.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() { @Override public void repeatAction() { numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) - 1)); } }, 50); //连续加 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.很多时候,长按的事件和普通点击的事件是一样的(也就是说,长按相当于连续的快速点击)。这种情况下,自定义Button可以更加简洁:即在长按时,连续的调用普通的OnClickListener。
public class LongClickButton2 extends Button { 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(); } 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); } } } private static class MyHandler extends Handler { private WeakReferenceref; MyHandler(LongClickButton2 button) { ref = new WeakReference<>(button); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); LongClickButton2 button = ref.get(); if (button != null) { //直接调用普通点击事件 button.performClick(); } } } public void setIntervalTime(long intervalTime) { this.intervalTime = intervalTime; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。