Android Development Notes: rewrite button Method

Source: Internet
Author: User

Directly rewrite the following three methods in the subclass of the Activity class:
Copy codeThe Code is as follows: // Rewrite the onCreateOptionsMenu () method. The menu bar is displayed.
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}

// The rewrite back button. If you only need the rewrite return button, you can rewrite the onBackPressed () method in the Activity.

@ Override
Public void onBackPressed (){
// TODO Auto-generated method stub
Super. onBackPressed ();

Toast. makeText (MainActivity. this, "onBackPressed !! ", Toast. LENGTH_SHORT). show ();
}

// Rewrite Search button
// The onKeyDown () method. Multiple buttons can be rewritten: Number key, Menu key, and return key.
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU ){

Toast. makeText (MainActivity. this, "Search Menu", Toast. LENGTH_SHORT). show ();

}

Return super. onKeyDown (keyCode, event );
}

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.