Introduction to Android Development (ix) user interface 9.2 rewrite onkeydown ()

Source: Internet
Author: User

Users can interact with your UI in two levels, one at the activity level and the other in the view level. At the activity level, the activity class exposes some of the methods that you can rewrite. There are some common methods that you can rewrite in your activity:

OnKeyDown

OnKeyUp

onmenuitemselected

onmenuopened

The following example rewrites some of the methods in the activity, showing how users interact with a multitude of activity.

1. Create a project: Uiactivity.

2. Code in the Main.xml.

 <?xml version= "1.0 encoding=" Utf-8 "?> <linearlayout xmlns:android=" http://schemas.android.com/apk/ Res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "android:orientation=" ve Rtical "> <textview android:layout_width=" 214DP "android:layout_height=" Wrap_co Ntent "android:text=" Your Name "/> <edittext android:id=" @+id/txt1 "and Roid:layout_width= "214DP" android:layout_height= "wrap_content"/> <button androi D:id= "@+id/btn1" android:layout_width= "106DP" android:layout_height= "Wrap_content" Android:text = "OK"/> <button android:id= "@+id/btn2" android:layout_width= "106DP" an droid:layout_height= "Wrap_content" android:text= "Cancel"/> </linearlayout>

The code in the

3, Uiactivityactivity.java.

The public class Uiactivityactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Overr     
        IDE public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     
               
    Setcontentview (R.layout.main); @Override public boolean onKeyDown (int keycode, keyevent event) {Swit CH (keycode) {case KeyEvent.KEYCODE_DPAD_CENTER:Toast.makeText (Getbas     
                Econtext (), "Center is clicked", Toast.length_long). Show ();                     
            Break Case KeyEvent.KEYCODE_DPAD_LEFT:Toast.makeText (Getbasecontext (), "left     
                Arrow is clicked ", Toast.length_long). Show ();                     
            Break Case KEYEVENT.KEYCODE_DPAD_RIGHT:Toast.makeText (Getbasecontext (), "Right Arrow is Clicke     
                D ", Toast.length_long). Show ();                     
            Break Case KeyEvent.KEYCODE_DPAD_UP:Toast.makeText (Getbasecontext (), "Up Arro     
                W is clicked ", Toast.length_long). Show ();     
            Break Case KeyEvent.KEYCODE_DPAD_DOWN:Toast.makeText (Getbasecontext (), "Down     
                Arrow is clicked ", Toast.length_long). Show ();                     
        Break     
    return false; }     
         
}

4. Press F11 to Debug.

5. When the activity is loaded, enter some text into the edittext. Then, click the Down button in the direction key. Watch the information on the screen.

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.