Android menu construction skills

Source: Internet
Author: User

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:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Menu xmlns: android = "http://schemas.android.com/apk/res/android">
  3. <Item android: id = "@ + id/settings"
  4. Android: title = "@ string/settings_label"
  5. Android: alphabeticShortcut = "@ string/settings_shortcut"/>
  6. // Add more items here
  7. </Menu>

Add the following string resources in strings. xml under res/values:

 
 
  1. < string name="settings_label">Settings...< /string> 
  2. < string name="settings_title">Sudoku settings< /string> 
  3. < string name="settings_shortcut">s< /string> 
  4. < string name="music_title">Music< /string> 
  5. < string name="music_summary">Play background music< /string> 
  6. < string name="hints_title">Hints< /string> 
  7. < 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:

 
 
  1. @Override  
  2. public boolean onCreateOptionsMenu(Menu menu) {  
  3. super.onCreateOptionsMenu(menu);  
  4. MenuInflater inflater = getMenuInflater();  
  5. inflater.inflate(R.menu.menu, menu);  
  6. 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:

 
 
  1. @Override  
  2. public boolean onOptionsItemSelected(MenuItem item) {  
  3. switch (item.getItemId()) {  
  4. case R.id.settings:  
  5. startActivity(new Intent(this, Settings.class));  
  6. return true;  
  7. // More items go here (if any) ...  
  8. }  
  9. 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:

 
 
  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < PreferenceScreen 
  3. xmlns:android="http://schemas.android.com/apk/res/android"> 
  4. < CheckBoxPreference 
  5. android:key="music" 
  6. android:title="@string/music_title" 
  7. android:summary="@string/music_summary" 
  8. android:defaultValue="true" /> 
  9. < CheckBoxPreference 
  10. android:key="hints" 
  11. android:title="@string/hints_title" 
  12. android:summary="@string/hints_summary" 
  13. android:defaultValue="true" /> 
  14. < /PreferenceScreen> 

2) Create the Settings. java class. The Code is as follows:

 
 
  1. package org.example.sudoku;  
  2. import android.os.Bundle;  
  3. import android.preference.PreferenceActivity;  
  4. public class Settings extends PreferenceActivity {  
  5. @Override  
  6. protected void onCreate(Bundle savedInstanceState) {  
  7. super.onCreate(savedInstanceState);  
  8. addPreferencesFromResource(R.xml.settings);  
  9. }  

So far, a complete Android menu has been created ~.

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.