Back to project page BluetoothSppPro.
The source code is released under:
Apache License
If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mobi.dzs.android.control.button; //from w w w . j a v a2s .c om import android.content.Context; import android.os.SystemClock; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.Button; /** * ??????? * ??????????????? * @author jerryli lijian@dzs.mobi * @see ??????????????bindListener()????????????????????? * ?????????? * */ public class RepeatingButton extends Button{ /**???????? */ private long mStartTime; /**?????????????????*/ private int mRepeatCount = 0; /**?????????????????*/ private ButtonPassListener mListener = null; /**Timer?????????????????500ms????*/ private long mInterval = 500; public RepeatingButton(Context context) { this(context, null); } public RepeatingButton(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.buttonStyle); } public RepeatingButton(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); setFocusable(true); //??????? setLongClickable(true); //???????? } /** * ????????????? * @param long interval ??????ms * @param ButtonPassListener l ????????????? * @return void * */ public void bindListener(ButtonPassListener l, long lHold_feq){ this.mListener = l; this.mInterval = lHold_feq; } /**???????????*/ public void setRepeatFreq(long interval){ this.mInterval = interval; } /** * ?????????? * @return boolean * */ @Override public boolean performLongClick(){ this.mStartTime = SystemClock.elapsedRealtime(); //??????????? this.mRepeatCount = 0; post(this.mRepeater); //?????????????? return true; } /** * ????????????? * @param event MotionEvent ??????? * @return boolean * */ @Override public boolean onTouchEvent(MotionEvent event){ if (event.getAction() == MotionEvent.ACTION_UP){ removeCallbacks(this.mRepeater); //?????????? this.doUp(); //?????????? }else if (event.getAction() == MotionEvent.ACTION_DOWN ){ //??????????? this.doDown(); } return super.onTouchEvent(event); } /**?????????????*/ private Runnable mRepeater = new Runnable(){ //?????????? /** ??????????????????????????????????? */ private long lFirstRunTime = mStartTime + mInterval; /** ????? */ public void run(){ if (SystemClock.elapsedRealtime() > lFirstRunTime)//????????????????????? doRepeat(false); //?????? if (isPressed()) //????????????????????????????????? postDelayed(this, mInterval); //?????????????? } }; /** * ??????????????? * @param last boolean ???????????????? * */ public void doRepeat(boolean last){ if (this.mListener != null) this.mListener.onRepeat( this, SystemClock.elapsedRealtime() - this.mStartTime, last ? -1 : this.mRepeatCount++ ); } /** * ?????????????? * */ public void doUp(){ if (this.mListener != null) this.mListener.onUp(this); } /** * ?????????????? * */ public void doDown(){ if (this.mListener != null) this.mListener.onDown(this); } }