//ActivityMenu.java
package android.ActivityUI;
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 ActivityMenu extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
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(ActivityMenu.this, ActivityAbout.class);
startActivity(intent);
ActivityMenu.this.finish();
break;
case R.id.exit:
ActivityMenu.this.finish();
break;
}
return true;
}
}
//R.layout.menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
//ActivityAbout.java
package android.ActivityUI;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ActivityAbout extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText("welcome :haha ,zhong yu cheng gong le !");
}
}
//about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="TextView"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
//R.menu.menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/about"
android:title="about">
</item>
<item
android:id="@+id/exit"
android:title="exit">
</item>
</menu>
点击Menu按钮后:
点击MENU键:
点击about后:
点击exit就返回Menu按钮所在窗口