1. Introduction to API documentation for Default_keys_shortcut
Use with Setdefaultkeymode (int) to execute a menu shortcut in default key handling.
That's, the user does not need to hold down the menu key to execute menu shortcuts.
Literally, it means that the default key input is processed as a menu shortcut.
In other words, the user does not need to press the menu button, you can handle the menus shortcut keys, it sounds very magical, is it so?
2. Write the sample program
We write a program to verify its function, first create a new project, and set the default key mode to Default_keys_shortcut
Package Com.silenceburn;
Import android.app.Activity;
Import Android.os.Bundle;
The public class Menushortcuttester extends activity {/** called the ' when ' is the ' The activity ' is a
-a-created
>public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Setdefaultkeymode (default_keys_shortcut);
}
Add an id attribute to the TextView in the default Main.xml, and then we'll use the menu option to control the color of the line.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<textview
android:id= "@+id/mytext" android:layout_width= "Fill_"
Parent "
android:layout_height=" wrap_content "
android:text=" @string/hello "
/>
</ Linearlayout>
Use Findviewbyid to get the text object with the ID defined in the previous step, and save its reference in member variable B.
Rewrite the Onprepareoptionsmenu method, add our own menu items, and register the shortcut keys while increasing the response events for menu clicks.
package Com.silenceburn;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.MenuItem.OnMenuItemClickListener;
Import Android.widget.TextView;
The public class Menushortcuttester extends activity {/** called the ' when the ' is ' the activity ' is a-created B; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancest
ATE);
Setcontentview (R.layout.main);
b = (TextView) This.findviewbyid (R.id.mytext);
Setdefaultkeymode (Default_keys_shortcut); @Override public boolean Onprepareoptionsmenu (Menu menu) {//TODO auto-generated method
Stub super.onprepareoptionsmenu (menu);
Menu.removeitem (0);
Menu.removeitem (1); Menu.add (0, 0, 0, "one"). Setshortcut (' 0 ', ' 0 '). SetONmenuitemclicklistener (New Onmenuitemclicklistener () {@Override public boolean Onmenuitem Click (MenuItem Item) {//TODO auto-generated Method stub B.setbackgroundcolor (Android . Graphics.
color.red);
return true;
}});
Menu.add (0, 1, 0, "two"). Setshortcut (' 1 ', ' 1 '). Setonmenuitemclicklistener (New Onmenuitemclicklistener () { @Override public boolean Onmenuitemclick (MenuItem Item) {//TODO auto-generated method
Stub b.setbackgroundcolor (Android.graphics.Color.GREEN);
return true;
}});
return true; }
}