OnKeyDown Invalid issue in Android tabactivity

Source: Internet
Author: User

@Override
public boolean onKeyDown (int keycode, keyevent event)
{

Press the Back button on the keyboard
if (keycode = = Keyevent.keycode_back)
{
New Alertdialog.builder (Loginactivity.this). Settitle ("hint")
. Setmessage ("Confirm exit?"). ")
. SetIcon (R.drawable.icon)
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener ()
{
@Override
public void OnClick (Dialoginterface dialog,
int which)
{
Android.os.Process.killProcess (Android.os.Process.myPid ()); End Process
}
})
. Setnegativebutton ("Cancel",
New Dialoginterface.onclicklistener ()
{
@Override
public void OnClick (Dialoginterface dialog,
int which)
{

}
})
. Show ();
return true;
}
Else
{
Return Super.onkeydown (KeyCode, event);
}
}

At this time in tabactivity unable to monitor keyevent,onkeydown conflict.

Workaround:

1. When inheriting tabactivity, the students are not onkeydown method useless, it should be a conflict, you can use the Dispatchkeyevent method

@Override
public boolean dispatchkeyevent (KeyEvent event)
{
if (event.getkeycode () = = Keyevent.keycode_back && event.getrepeatcount () = = 0&&event.getaction () = = Keyevent.action_down) {
New Alertdialog.builder (Mainactivity.this). Settitle ("hint")
. Setmessage ("Confirm exit?"). ")
. SetIcon (R.drawable.icon)
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener ()
{
@Override
public void OnClick (Dialoginterface dialog,
int which)
{
New Requestcommandservice (). Logoutreq ();
Android.os.Process.killProcess (Android.os.Process.myPid ()); End Process
}
})
. Setnegativebutton ("Cancel",
New Dialoginterface.onclicklistener ()
{
@Override
public void OnClick (Dialoginterface dialog,
int which)
{

}
})
. Show ();
return false;
}

Return Super.dispatchkeyevent (event);

}

Can solve the problem, can monitor the KeyEvent event, but will listen two times, pop two alertdialog.

So if you need to add event.getaction () = = Keyevent.action_down is OK

2. You can also go to Tabactivity's child activity with onkeydown

3. When there is double tabactivity can be used

public boolean onKeyDown (int keycode, keyevent event) {
if (Keycode==keyevent.keycode_back && event.getrepeatcount () ==0) {
Touchlistenerutils.back (This.getparent (). GetParent ());//getparent (). refers to the parent class
return false;
}else{
Return Super.onkeydown (KeyCode, event);
}
}

OnKeyDown not working in Android tabactivity

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.