In the Android mobile phone operating system, you can use many methods to implement the same function. This depends on the personal interests of programmers and the applicable environment. For example, you can use multiple methods such as dynamic to create an Android menu.
Today, I learned how to construct an Android Menu Using xml files. First, create a menu folder under res, and create a menu. xml folder with the following content:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Menu xmlns: android = "http://schemas.android.com/apk/res/android">
- <Item android: id = "@ + id/settings"
- Android: title = "@ string/settings_label"
- Android: alphabeticShortcut = "@ string/settings_shortcut"/>
- // Add more items here
- </Menu>
Add the following string resources in strings. xml under res/values:
- < string name="settings_label">Settings...< /string>
- < string name="settings_title">Sudoku settings< /string>
- < string name="settings_shortcut">s< /string>
- < string name="music_title">Music< /string>
- < string name="music_summary">Play background music< /string>
- < string name="hints_title">Hints< /string>
- < string name="hints_summary">Show hints during play< /string>
As in the previous article, add the following code to override the onCreateOptionsMenu event of the base class in the activity class:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu, menu);
- return true;
- }
In this way, an Android menu has been created. Note that the MenuInflater class is used here to read the xml file and create a menu using the inflate method of this class. Note that this xml menu has only one item. If you need more items, you can add them later.
The next step is to implement the events of various menu items. Override the onOptionsItemSelected method of the base class in the activity class:
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.settings:
- startActivity(new Intent(this, Settings.class));
- return true;
- // More items go here (if any) ...
- }
- return false;
- }
In the response Android menu event, we create a new activity. This activity is presented through the Settings class. We know that there are two ways to present an activity: 1) Implement through code layout 2) Implement through xml files. Each method has advantages and disadvantages. Here we use an xml file to present a view. The procedure is as follows:
1) First, create an xml folder under the res file, and create the Settings. xml file under the xml folder. The Settings. xml file is as follows:
- < ?xml version="1.0" encoding="utf-8"?>
- < PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- < CheckBoxPreference
- android:key="music"
- android:title="@string/music_title"
- android:summary="@string/music_summary"
- android:defaultValue="true" />
- < CheckBoxPreference
- android:key="hints"
- android:title="@string/hints_title"
- android:summary="@string/hints_summary"
- android:defaultValue="true" />
- < /PreferenceScreen>
2) Create the Settings. java class. The Code is as follows:
- package org.example.sudoku;
- import android.os.Bundle;
- import android.preference.PreferenceActivity;
- public class Settings extends PreferenceActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.settings);
- }
- }
So far, a complete Android menu has been created ~.