Monitoring and processing of return keys in Android apps

Source: Internet
Author: User

Mainactivity as follows:

Package com.testnbackpressed; Import Android.os.Bundle; Import android.view.KeyEvent; Import android.app.Activity;     /** * Demo Description: * Handling back Key Press event * * NOTE: * The following two ways not to use * * * public class Mainactivity extends Activity {@Override         protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     Setcontentview (R.layout.main);     /** * Monitor the Back key press event, Method 1: * Note: * super.onbackpressed () will automatically call the finish () method to close * Current activity.         * To block the back keyboard, comment on the line code */@Override public void onbackpressed () {super.onbackpressed ();            System.out.println ("Press back Key onbackpressed ()");    }/** * listens to the back key press event, Method 2: * Note: * The return value indicates whether the event can be fully processed * returns false here, so the event will continue to propagate.    * The return value here is subject to availability in the specific project. */@Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back               ) {System.out.println ("Press back Key OnKeyDown ()"); RetuRN false;         }else {return Super.onkeydown (KeyCode, event);         }} @Override protected void OnDestroy () {Super.ondestroy ();     SYSTEM.OUT.PRINTLN ("Executive OnDestroy ()"); }       }

Main.xml as follows:

<relativelayout      xmlns:android= "http://schemas.android.com/apk/res/android"     xmlns:tools= "http// Schemas.android.com/tools "     android:layout_width=" match_parent "     android:layout_height=" Match_parent "     >        <textview         android:layout_width= "wrap_content"         android:layout_height= "Wrap_content"         android:text= "         android:layout_centerinparent=" true "android:textsize=" for the two processing methods of the back key "         20sp"    />    </RelativeLayout>

  

Monitoring and processing of return keys in Android apps

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.