Android custom buttons enable long-pressing
Use a Custom BUTTON to write a LongTouchBtn class and execute the onTouchEvent event when you press it. This event uses the callback function to implement the long press function!
XML:
Activity:
Public class MainActivity extends Activity {private TextView Tv1; private LongTouchBtn Btn1; private int num = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Tv1 = (TextView) findViewById (R. id. tv1); Btn1 = (LongTouchBtn) findViewById (R. id. btn2); Btn1.setOnClickListener (new View. onClickListener () {@ Overridepublic void OnClick (View arg0) {Log. I ("huahua", "Custom button processing click") ;}}); Btn1.setOnLongClickListener (new View. onLongClickListener () {@ Overridepublic boolean onLongClick (View v) {Log. I ("huahua", "one-time Custom button handling duration"); return false ;}}); /*** This is a custom interface dedicated to handling long-press logic * @ param listener * listeners. * @ Param time * 2nd parameters are passed into 1000, indicating that onLongTouch () method is processed once per second */Btn1.setOnLongTouchListener (new LongTouchListener () {@ Overridepublic void onLongTouch () {num ++; tv1.setText (num + ""); Log. I ("huahua", "holding long") ;}, 1000 );}}
Baihua. btnlongtouch. LongTouchBtn:
Public class LongTouchBtn extends Button {/*** record whether the current custom Btn is pressed */private boolean clickdown = false;/*** pull-down and refresh callback interface */private LongTouchListener mListener; /*** how many milliseconds does the button last for processing the callback Method */private int mtime; /*** constructor * @ param context * @ param attrs */public LongTouchBtn (Context context, AttributeSet attrs) {super (context, attrs ); // TODO Auto-generated constructor stub}/*** process touch events */@ Overridepublic boo Lean onTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {clickdown = true; new longtouchtask(cmd.exe cute (); Log. I ("huahua", "press");} else if (event. getAction () = MotionEvent. ACTION_UP) {clickdown = false; Log. I ("huahua", "Bounce");} return super. onTouchEvent (event);}/*** specifies the number of milliseconds for the current thread to sleep. ** @ Param time * specifies the duration of sleep of the current Thread, in milliseconds */private void sleep (int time) {try {Thread. sleep (time);} catch (InterruptedException e) {e. printStackTrace () ;}/ *** process long-pressed tasks */class LongTouchTask extends AsyncTask
{@ Overrideprotected Void doInBackground (Void... params) {while (clickdown) {sleep (mtime); publishProgress (0);} return null ;}@ Overrideprotected void onPostExecute (Void result) {}@ Overrideprotected void onProgressUpdate (Integer... values) {mListener. onLongTouch () ;}}/*** registers a listener for the Long-pressed btn control. ** @ Param listener * listener implementation. * @ Param time * How many milliseconds to process a callback Method */public void setOnLongTouchListener (LongTouchListener listener, int time) {mListener = listener; mtime = time ;} /*** long-press listener interface. You should register this listener to obtain the callback where the button is long-pressed. */Public interface LongTouchListener {/*** callback method for handling long presses */void onLongTouch ();}}