@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