Menu01.java:
package com.Menu01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class Menu01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
int item_id=item.getItemId();
switch(item_id)
{
case R.id.about:
Intent intent=new Intent();
intent.setClass(Menu01.this, Menu02.class);
startActivity(intent);
Menu01.this.finish();
break;
case R.id.exit:
Menu01.this.finish();
break;
}
return true;
}
}
Menu02.java:
package com.Menu01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Menu02 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public boolean onCreateOptionMenu(Menu menu)
{
menu.add(0, 0, 0, R.string.ok);
menu.add(0, 1, 1, R.string.back);
return true;
}
public boolean onOpetionsItemSelected(MenuItem item)
{
int item_id=item.getItemId();
switch(item_id)
{
case 0:
case 1:
Intent intent=new Intent();
intent.setClass(Menu02.this, Menu01.class);
startActivity(intent);
Menu02.this.finish();
break;
}
return true;
}
}
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/about"
android:title="关于"
/>
<item
android:id="@+id/exit"
android:title="退出"
/>
</menu>