码迷,mamicode.com
首页 > 移动开发 > 详细

Android:OptionMenu

时间:2015-08-03 19:04:36      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

 

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>

  

Android:OptionMenu

标签:

原文地址:http://www.cnblogs.com/Sunnor/p/4699990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!