Android (Android) monitor button length by event _android

Source: Internet
Author: User

1,key--Entity key

There are fewer physical buttons on the phone.

Common there are keycode_volume_down/up keycode_power keycode_back keycode_home keycode_menu

The following three methods of overloading a parent class in an activity handle key events

 public boolean onKeyDown (int keycode, keyevent event): Public
boolean onKeyUp (int keycode, keyevent event):
PU Blic boolean onkeylongpress (int keycode, keyevent event):

It is obvious that long press events are usually handled in the Onkeylongpress function.

What is the specific way to do it, please look at the following steps:

The first step is to determine the number of event.getrepeatcount in the OnKeyDown function (in fact, the long press is triggered by a series of onKeyDown events).

 @Override public boolean onKeyDown (int keycode, keyevent event) {case Keyevent.keycod
 E_volume_down:case KeyEvent.KEYCODE_VOLUME_UP:if (mcameraappview.getvisibility ()!= view.visible) {return false;
 LOG.V (TAG, "OnKeyDown event.getrepeatcount ()" +event.getrepeatcount ());
 if (event.getrepeatcount () = = 0) {event.starttracking ();
 return true;
 return true;
 Return Super.onkeydown (KeyCode, event); If you keep pressing, you can see OnKeyDown event.getrepeatcount () 0 OnKeyDown event.getrepeatcount () 1 OnKeyDown Event.getrepea by playing log Tcount () 2 OnKeyDown event.getrepeatcount () 3 OnKeyDown event.getrepeatcount () 4 OnKeyDown Event.getrepeatcount () 5 on KeyDown Event.getrepeatcount () 6 OnKeyDown Event.getrepeatcount () 7 OnKeyDown event.getrepeatcount () 8 OnKeyDown event. Getrepeatcount () 9 OnKeyDown event.getrepeatcount () OnKeyDown event.getrepeatcount () OnKeyDown Event.getrepeatcou 
NT () ... 

The second step is to overload the Onkeylongpressed function, in which you can add your processing

@Override Public
boolean onkeylongpress (int keycode, keyevent event) {
log.v (TAG, "key long pressed keycode =" +k Eycode);
Switch (keycode) {case
keyevent.keycode_volume_up: Case
Keyevent.keycode_volume_down:
log.v (TAG, " Keycode_volume_up/down long pressed ");
Locklongpresskey = true;
Onshutterbuttonlongpressed ();
return true;
Default: Break
;
} return
super.onkeylongpress (KeyCode, event);
}

In the third step, if the same key is handled in the ONKEYUP function, the processing of the key in Onkeylongpress and onKeyUp needs to be mutually exclusive.

public boolean onKeyUp (int keycode, keyevent event) {
switch (keycode) {case
keyevent.keycode_volume_up:
C ASE Keyevent.keycode_volume_down:
log.v (TAG, "OnKeyUp event.getrepeatcount ()" +event.getrepeatcount ());
if (locklongpresskey) {
Locklongpresskey = false; return
true;
}
if (event.getrepeatcount () = 0) {
    log.v (TAG, "Prepare for onshutterbuttonclick ...");
if (menablerecordbtn && mreviewimage.getvisibility ()!= view.visible
&& Mbglearningmessageframe.getvisibility () = = View.gone) {
if (Mshutterbutton!= null && Mshutterbutton.isenabled ()) {
onshutterbuttonfocus (false);
collapsecameracontrols ();// Fulin@20121101@fix PD1218 b121027-224
Onshutterbuttonclick ();}}}
if ( Mcameraappview.getvisibility ()!= view.visible) {return
false;
} return
true;
Er.onkeyup (KeyCode, event);
}

The reason for mutexes is that after a long event is processed, the ONKEYUP function is executed when the key is released. And these two are different for the same buttons that you want to implement.

Solid here adds a private Boolean locklongpresskey = false; variables, which are processed in the Onkeylongpress function, are not processed in the onkeyup.

2, Button

The custom button length event is done by implementing the Onlongclicklistener interface.

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnLongClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class MyActivity extends activity implements onlongclicklistener{button
button;//Declaration button Reference public
void O  Ncreate (Bundle savedinstancestate) {//rewritten OnCreate method
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Button = (button) This.findviewbyid (R.id.button);//Get a reference to the buttons
button.settextsize ();
Button.setonlongclicklistener (this);/Registered Listener
} public
Boolean Onlongclick (View v) {//implement method in interface
if (v = b Utton) {//When the button is pressed
toast.maketext (
This, "Long pressed button",
toast.length_short
). Show ()//  Show Prompt
} return
false;
}
}

Above, is a small series on the Android Monitor button long according to the incident detailed, welcome to add, thank you for your support!

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.