Android custom buttons enable long-pressing

Source: Internet
Author: User

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 ();}}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.