Android_Menu Demo

Eclipse Version:Galieo-SR2-win32

ADT Version:14.0.0

SDK Version :1.5

JDK Version:Sun 1.6.0_26


效果图:Android_Menu Demo_第1张图片Android_Menu Demo_第2张图片


AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.travis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
        
        <activity android:label="@string/app_name" android:name=".Splash" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:label="@string/app_name" android:name=".Menu">
            <intent-filter >
                <action android:name="com.example.travis.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
	    </application>

</manifest>

cool_menu.xml:(layout布局文件,在res/menu目录下,menu目录为新建目录)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:title="About us"
        android:id="@+id/aboutUs"
        android:numericShortcut="1"
        android:alphabeticShortcut="a">
    </item>
	<item android:title="Preferences"
        android:id="@+id/preferences" >
    </item>
    <item android:title="Exit"
        android:id="@+id/exit" >
    </item>
</menu>


Menu.java(包含进入程序后的List列表代码):

package com.example.travis;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {

	String classes[] = { "StartingPoint", "TextPlay", "Email", "Camera",
			"Data", "GFX", "GFXSurface", "SoundStuff", "Slider", "Tabs", 
			 "SimpleBrowser", "Flipper", "SharedPrefs", "ExternalData", "InternalData"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//fullscreen
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setListAdapter(new ArrayAdapter<String>(Menu.this,
				android.R.layout.simple_list_item_1, classes));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		String cheese = classes[position];
		try {
			Class ourClass = Class.forName("com.example.travis." + cheese);
			Intent ourIntent = new Intent(Menu.this, ourClass);
			startActivity(ourIntent);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater blowUp = getMenuInflater();
		blowUp.inflate(R.menu.cool_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.aboutUs:
			Intent i = new Intent("com.example.travis.ABOUT");
			startActivity(i);
			break;
		case R.id.preferences:
			Intent p = new Intent("com.example.travis.PREFS");
			startActivity(p);
			break;
		case R.id.exit:
			finish();
			break;
		}
		return true;
	}

}

如果你有任何疑问,欢迎留言一起交流!


你可能感兴趣的:(android,ListView,application,action,menu,encoding)