Android:OptionMenu

时间:2021-11-19 06:43:11

MainActivity:

package com.example.optionmenu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } /**
* onCreateOptionMenu(Menu menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// this.getMenuInflater().inflate(R.menu.main, menu);
/*
* add(int groupId, int itemId, int order, CharSequence title)
* Add a new item to the menu.
*/
MenuItem item = menu.add(1, 100, 1, "准备跳转"); item.setTitle("aaaaaa");
item.setIcon(R.drawable.ic_launcher);
menu.add(1, 101, 1, "菜单二");
menu.add(1, 102, 1, "菜单三");
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Intent intent = new Intent(this,SecondActivity.class);
// System.out.println("=====准备item.setIntent(intent)======");
// item.setIntent(intent);
// System.out.println("=====已经item.setIntent(intent)======");
// return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case 100:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
System.out.println("======准备item.setIntent(intent)2======");
item.setIntent(intent);
break; case 101:
Toast.makeText(MainActivity.this, "点击了菜单一", Toast.LENGTH_SHORT)
.show();
break;
case 102:
Toast.makeText(MainActivity.this, "点击了菜单二", Toast.LENGTH_SHORT)
.show();
break;
}
return super.onOptionsItemSelected(item);
}
}

  SecondActivity:

package com.example.optionmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
System.out.println("====== SecondActivity.onCreate()方法=========");
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// this.getMenuInflater().inflate(R.menu.main, menu);
/*
* add(int groupId, int itemId, int order, CharSequence title)
* Add a new item to the menu.
*/
System.out.println("======onCreateOptionMenu(Menu menu)方法=====");
menu.add(1, 101, 1, "fuck01");
menu.add(1,102,1,"fuck02"); return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("======onOptionsItemSelected(MenuItem item)方法=====");
int itemid = item.getItemId();
if(itemid==101){
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
item.setIntent(intent);//跳转,实现activity的切换
}else if(itemid==102){
Toast.makeText(SecondActivity.this, "i am the secondActivity's item2", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
} @Override
protected void onStart() { super.onStart();
System.out.println("====== SecondActivity.onStart()方法=========");
} @Override
protected void onResume() {
super.onResume();
System.out.println("====== SecondActivity.onResume()方法=========");
} @Override
protected void onRestart() {
super.onRestart();
System.out.println("====== SecondActivity.onRestart()方法=========");
} @Override
protected void onStop() {
super.onStop();
System.out.println("====== SecondActivity.onStop()方法=========");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("====== SecondActivity.onDestroy()方法=========");
}
}

  menu-->main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.optionmenu.MainActivity" > <item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/> </menu>

  menu-->second.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.example.optionmenu.SecondActivity">
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:title="我是第二个activity的title"
app:showAsAction="never"/> </menu>