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.