Android学习笔记之Menu一级菜单、二级菜单的使用

(1)布局文件没有做更改

(2)在res--menu目录下的main.xml文件中代码如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 一级菜单 -->
    <item
        android:id="@+id/file"
        android:title="@string/file">

        <!-- 二级菜单 -->
        <menu>
            <item
                android:id="@+id/create"
                android:title="@string/create">
            </item>
            <item
                android:id="@+id/open"
                android:title="@string/open">
            </item>
        </menu>
    </item>
    <!-- 一级菜单 -->
    <item
        android:id="@+id/chioce"
        android:title="@string/chioce">

        <!-- 二级菜单 -->
        <menu>
            <item
                android:id="@+id/create1"
                android:title="@string/create">
            </item>
            <item
                android:id="@+id/open1"
                android:title="@string/open">
            </item>
        </menu>
    </item>

</menu>

(3)values目录下的string.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Menu_submenu</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="file">文件</string>
    <string name="create">新建</string>
    <string name="open">打开</string>
    <string name="chioce">选项</string>

</resources>

(3)MainActivity.java代码如下:

package com.example.menu_submenu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/*
	 * 处理一级菜单
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.file:
			Toast.makeText(MainActivity.this, "一级菜单file..", Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.chioce:
			Toast.makeText(MainActivity.this, "一级菜单chioce..",
					Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * 处理二级菜单的
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.create1:
			Toast.makeText(MainActivity.this, "处理二级菜单create1..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.open1:
			Toast.makeText(MainActivity.this, "处理二级菜单create1..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.create:
			Toast.makeText(MainActivity.this, "处理二级菜单create..",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.open:
			Toast.makeText(MainActivity.this, "处理二级菜单open..",
					Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
}

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。