To create the Menu, you need to override the onCreateOptionsMenu( ) method of the
Activity. The method onCreateOptionsMenu( ) is a Boolean method that is called when
the user first selects the Menu Button. You will use this method to build your Menu and
add your selection items to it. Add the following code to AndroidViews.java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
}
You will add the code to create the Menu inside the onCreateOptionsMenu( ) method.
The items that you need to add to the Menu are the Views that you are going to create
in this project. The following is the list of View names that you will need to add to
the Menu:
● AutoComplete
● Button
● CheckBox
● EditText
● RadioGroup
● Spinner
In the preceding code that you created to override the onCreateOptionsMenu( )
method, you passed in a Menu variable called menu. This variable represents the actual
menu item that is created on the Android interface.
To add your list of items to the Menu, you will use the menu.add( ) method. The
syntax for this call is as follows:
menu.add(<group>,<id>,<title>)
The parameter group is used to associate the menu items. You will not be using group
in this example. However, the value is very important. The parameter id is used to
determine what menu item was selected. Finally, the parameter title is the text that will
be displayed in the Menu.
Add the following code to the onCreateOptionsMenu( ) method:
menu.add(0, 0, "AutoComplete");
menu.add(0, 1, "Button");
menu.add(0, 2, "CheckBox");
menu.add(0, 3, "EditText");
menu.add(0, 4, "RadioGroup");
menu.add(0, 5, "Spinner");
Your full AndroidViews.java file should now look like this:
package android_programmers_guide.AndroidViews;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class AndroidViews extends Activity {
/** Called when the Activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, "AutoComplete");
menu.add(0, 1, "Button");
menu.add(0, 2, "CheckBox");
menu.add(0, 3, "EditText");
menu.add(0, 4, "RadioGroup");
menu.add(0, 5, "Spinner");
return true;
}
}
This is exactly what you wanted to accomplish. However, try clicking any of the
options in the Menu. You do not have anything in your Activity that handles events
when the user selects a Menu item.
The method you need to override to handle the calls to the Menu items is
onOptionsItemSelected( ). Again, like onCreateOptionsMenu( ), onOptionsItemSelected( )
is a Boolean method that you need to override with the specific code to be executed when
a Menu item is selected. The override code should look like this:
@Override
public boolean onOptionsItemSelected(Menu.Item item){
}
There is one problem with this code: onOptionsItemSelected( ) is a general method
that is called when any menu item is selected. You need to give onOptionsItemSelected( )
a way to differentiate between the menu items and execute code accordingly. Therefore,
use a switch/case statement to help the method select between the menu items. When you
created the menu items, you specified a series of numbers from 0 to 5 as the values for
your menu items. You can use a call to getId( ) in your case statement to determine which
menu item was selected:
switch (item.getId()) {
case 0:
return true;
case 1:
return true;
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 5:
return true;
}
return true;
In this case statement, the action for each id is currently set to return true. This
will not do anything but hold open the area where you need to add code. Your
AndroidViews.java file is now ready for use to create Activities that can be launched
by the new menu system. The full code of AndroidViews.java should look like this:
package android_programmers_guide.AndroidViews;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class AndroidViews extends Activity {
/** Called when the Activity is first created. /
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
/** Add one menu item for each View in our project */
menu.add(0, 0, "AutoComplete");
menu.add(0, 1, "Button");
menu.add(0, 2, "CheckBox");
menu.add(0, 3, "EditText");
menu.add(0, 4, "RadioGroup");
menu.add(0, 5, "Spinner");
return true;
}
/** Override onOptionsItemSelected to execute code for each
menu item */
@Override
public boolean onOptionsItemSelected(Menu.Item item){
/** Select statement to handle calls
to specific menu items */
switch (item.getId()) {
case 0:
return true;
case 1:
return true;
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 5:
return true;
}
return true;
}
}
With AndroidViews.java complete, you can focus on creating your other Activities. In
the following sections, you will create one Activity for each View in your project and add
the code to launch that view's Activity in your case statement.
0 件のコメント:
コメントを投稿