Android创建Menu菜单实例

本文实例讲述了Android创建Menu菜单的方法。分享给大家供大家参考。具体方法如下:

package com.test;

import android.app.Activity;

import android.content.Intent; import android.os.Bundle; import android.provider.Settings; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem;

public class TestandroidActivity extends Activity {     @Override  public boolean onCreateOptionsMenu(Menu menu) {   // TODO Auto-generated method stub    super.onCreateOptionsMenu(menu);    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.menu, menu);    return true;     }

 /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);     }

 @Override  public boolean onOptionsItemSelected(MenuItem item) {   // TODO Auto-generated method stub   switch(item.getItemId())   {   case R.id.settings:    startActivity(new Intent(this,com.test.Settings.class));    return true;   }   return false;  } }

package com.test;

import android.os.Bundle; import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {

 @Override  protected void onCreate(Bundle savedInstanceState) {   // TODO Auto-generated method stub   super.onCreate(savedInstanceState);   addPreferencesFromResource(R.xml.settings);  } }

相关配置文件如下:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference android:key="music" android:summary="@string/music_summary" android:defaultValue="true" android:title="@string/music_title"/>

    <CheckBoxPreference android:key="hints" android:summary="@string/hints_summary" android:title="@string/hints_title"/>

</PreferenceScreen>

希望本文所述对大家的Android程序设计有所帮助。