Long press event for the listening button in Android

Source: Internet
Author: User

1,key--Entity buttons,

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)

public 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.keycode_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 by playing log

OnKeyDown Event.getrepeatcount () 0

OnKeyDown Event.getrepeatcount () 1

OnKeyDown Event.getrepeatcount () 2

OnKeyDown Event.getrepeatcount () 3

OnKeyDown Event.getrepeatcount () 4

OnKeyDown Event.getrepeatcount () 5

OnKeyDown Event.getrepeatcount () 6

OnKeyDown Event.getrepeatcount () 7

OnKeyDown Event.getrepeatcount () 8

OnKeyDown Event.getrepeatcount () 9

OnKeyDown Event.getrepeatcount () 10

OnKeyDown Event.getrepeatcount () 11

OnKeyDown Event.getrepeatcount () 12

......

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 =" +keycode);

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.

@Override

public boolean onKeyUp (int keycode, keyevent event) {

Switch (keycode) {

Case KEYEVENT.KEYCODE_VOLUME_UP:

Case 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;

}

Return Super.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;//A reference to the declaration buttons

public void OnCreate (Bundle savedinstancestate) {//overridden OnCreate method

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Button = (button) This.findviewbyid (R.id.button);//Get a reference to the buttons

Button.settextsize (20);

Button.setonlongclicklistener (this);//Register Monitor

}

public boolean Onlongclick (View v) {//method in implementation interface

if (v = = button) {//When the button is pressed

Toast.maketext (

This

"Pressed the button for a long time,"

Toast.length_short

). Show ()//Display prompt

}

return false;

}

}

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.