In Android, the button is long-pressed.

Source: Internet
Author: User

For many games that use screen control, you generally need to consider the long-press event. For example, in a gossip game, you need to use the long-press launch weapon. In combination with the android button model, we implement a long-press of a button with an image, for a clearer display principle, Android developer uses imagebutton as the base class.

  1. Public class repeatingimagebutton extends imagebutton {
  2. Private long mstarttime; // start with a long record
  3. Private int mrepeatcount; // repeated count
  4. Private repeatlistener mlistener;
  5. Private long minterval = 500; // timer trigger interval, that is, the press is calculated every 0.5 seconds.
  6. Public repeatingimagebutton (context ){
  7. This (context, null );
  8. }
  9. Public repeatingimagebutton (context, attributeset attrs ){
  10. This (context, attrs, Android. R. ATTR. imagebuttonstyle );
  11. }
  12. Public repeatingimagebutton (context, attributeset attrs, int defstyle ){
  13. Super (context, attrs, defstyle );
  14. Setfocusable (true); // allows focus
  15. Setlongclickable (true); // enable the long press event
  16. }
  17. Public void setrepeatlistener (repeatlistener L, long interval) {// implement repeated listener events
  18. Mlistener = L;
  19. Minterval = interval;
  20. }
  21. @ Override
  22. Public Boolean initialize mlongclick (){
  23. Mstarttime = systemclock. elapsedrealtime ();
  24. Mrepeatcount = 0;
  25. Post (mrepeater );
  26. Return true;
  27. }
  28. @ Override
  29. Public Boolean ontouchevent (motionevent event ){
  30. If (event. getaction () = motionevent. action_up) {// The principle of this method is the same as that of onkeyup. Here screen events are handled, and the onkeyup below processes physical button events on Android phones.
  31. Removecallbacks (mrepeater );
  32. If (mstarttime! = 0 ){
  33. Dorepeat (true );
  34. Mstarttime = 0;
  35. }
  36. }
  37. Return super. ontouchevent (event );
  38. }
  39. // Process the key or trackball press event of the navigation key event
  40. @ Override
  41. Public Boolean onkeydown (INT keycode, keyevent event ){
  42. Switch (keycode ){
  43. Case keyevent. keycode_dpad_center:
  44. Case keyevent. keycode_enter:
  45. Super. onkeydown (keycode, event );
  46. Return true;
  47. }
  48. Return super. onkeydown (keycode, event );
  49. }
  50. // When the button pops up, the notification duration is terminated.
  51. @ Override
  52. Public Boolean onkeyup (INT keycode, keyevent event ){
  53. Switch (keycode ){
  54. Case keyevent. keycode_dpad_center:
  55. Case keyevent. keycode_enter:
  56. Removecallbacks (mrepeater); // cancel repeated listener capture
  57. If (mstarttime! = 0 ){
  58. Dorepeat (true); // If the cumulative duration of a long press event is not 0, the long press is performed.
  59. Mstarttime = 0; // reset the timer
  60. }
  61. }
  62. Return super. onkeyup (keycode, event );
  63. }
  64. Private runnable mrepeater = new runnable () {// duplicate in the thread
  65. Public void run (){
  66. Dorepeat (false );
  67. If (ispressed ()){
  68. Postdelayed (this, minterval); // After the computing duration is pressed, the next accumulation is delayed.
  69. }
  70. }
  71. };
  72. Private void dorepeat (Boolean last ){
  73. Long Now = systemclock. elapsedrealtime ();
  74. If (mlistener! = NULL ){
  75. Mlistener. onrepeat (this, now-mstarttime, last? -1: mrepeatcount ++ );
  76. }
  77. }

The following is the definition of the repeated button listener interface. when calling the button, use the setrepeatlistener () method to implement the repeatlistener interface.

  1. Public interface repeatlistener {
  2. Void onrepeat (view V, long duration, int repeatcount); // The parameter is the user-passed button object, the parameter is the number of milliseconds of delay, and the third-bit repetition callback.
  3. }
  4. }

You can directly implement the repeatlistener interface in implements in your view.

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.