Tabactivity return key onbackpressed listener dispatchkeyevent listener

Source: Internet
Author: User

Tabactivity return key onbackpressed listener dispatchkeyevent listener

In the morning, I encountered a monitoring event of the tabactivity return key, which caused various problems. Early in the morning, Google finally found a solution by reading the document. The following is a solution based on others: We know that you can rewrite it in a common activity.Onkeydown (intkeycode, keyeventevent)The method listens to the mobile phone buttons. When the activity to be listened to is tabactivity (in fact, the listactivity is the same), this method does not work because the tabactivity does not obtain the focus, the focus is on one of the addtab child activities in tabactivity, so this method does not work. The homepage of the application being developed is a tabactivity. When you press the return key, the application will exit, which may be unintentional to the user. Therefore, when you press the return key, you need to give a prompt. General method, as mentioned above, rewriteOnkeydown (intkeycode, keyeventevent)It does not work at all. It is also found that the onbackpressed () method for the Return key is added after sdk2.0. Google has not found any good solutions for a long time. Open the SDK documentation and read it carefully.Dispatchkeyevent (keyeventevent)The following is a sample code. Note that you must addEvent. getaction () = keyevent. action_downJudging, because there are two events on the buttonsAction_downAndAction_up, That is, press and release. If this judgment is not added, the code will be executed twice, and the following code will play alertdialog twice.

[Java]View plaincopyprint?
  1. @ Override
  2. Public Boolean dispatchkeyevent (keyevent event ){
  3. If (event. getkeycode () = keyevent. keycode_back & event. getaction () = keyevent. action_down ){
  4. // Events to be monitored
  5. Alertdialog. Builder = new alertdialog. Builder (
  6. Mainactivity. This)
  7. . Seticon (R. drawable. logo) <span style = "font-size: 14px;"> // set the image </span>
  8. . Settitle ("prompt ")
  9. . Setmessage ("do you want to exit? ");
  10. Builder. setpositivebutton ("OK ",
  11. New onclicklistener (){
  12. Public void onclick (dialoginterface dialog, int which ){
  13. Mainactivity. This. Finish ();
  14. }
  15. });
  16. Builder. setnegativebutton ("cancel ",
  17. New onclicklistener (){
  18. Public void onclick (dialoginterface dialog,
  19. Int which ){
  20. Dialog. Cancel ();
  21. }
  22. });
  23. Builder. Create (). Show ();
  24. Return false;
  25. }
  26. Return super. dispatchkeyevent (event );
  27. }
@ Overridepublic Boolean dispatchkeyevent (keyevent event) {If (event. getkeycode () = keyevent. keycode_back & event. getaction () = keyevent. action_down) {// The Event alertdialog to be monitored. builder = new alertdialog. builder (mainactivity. this ). seticon (R. drawable. logo) // set the image. settitle ("prompt "). setmessage ("do you want to exit? "); Builder. setpositivebutton ("OK", new onclicklistener () {public void onclick (dialoginterface dialog, int which) {mainactivity. this. finish () ;}}); builder. setnegativebutton ("cancel", new onclicklistener () {public void onclick (dialoginterface dialog, int which) {dialog. cancel () ;}}); builder. create (). show (); Return false;} return Super. dispatchkeyevent (event );}

In addition, in the above Code, after mainactivity. This. Finish (), other child activities that come in with addtab in tabactivity will also be completed ();

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.