android menu学习

Activity01

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Activity01 extends Activity
{
Intent intent;


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

}
       //新建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.abl:
/* 新建一个Intent对象 */
   intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity01.this, Activity02.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity01.this.finish();
break;
case R.id.exit:
Activity01.this.finish();
break;
}
return true;
}
}

Activity02

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class Activity02 extends Activity {
Intent intent;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

}
public boolean onCreateOptionsMenu(Menu menu){

menu.add(0, 0, 0, "确定");
menu.add(0, 1, 1, "退出");
return true;

}
public boolean onOptionsItemSelected(MenuItem item){
int item_id = item.getItemId();

switch (item_id)
{
case 0:
case 1:
/* 新建一个Intent对象 */
intent= new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity02.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity02.this.finish();
break;
}
return true;

}

}

你可能感兴趣的:(android,OS)