zoukankan      html  css  js  c++  java
  • Android-高级UI控件_Menu菜单

    代码

    Activity01

    package com.lxt008;
    
    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 Activity01 extends Activity
    {
        /** 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();
            //设置menu界面为res/menu/menu.xml
            inflater.inflate(R.menu.menu, menu);
            return true;
        }
    
        /*处理菜单事件*/
        public boolean onOptionsItemSelected(MenuItem item)
        {
            //得到当前选中的MenuItem的ID,
            int item_id = item.getItemId();
    
            switch (item_id)
            {
                case R.id.about:
                    /* 新建一个Intent对象 */
                    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

    package com.lxt008;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class Activity02 extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            /* 设置显示main2.xml布局 */
            setContentView(R.layout.main2);
        }
    
        /*创建menu*/
        public boolean onCreateOptionsMenu(Menu menu)
        {
            //为menu添加内容
            menu.add(0, 0, 0, R.string.ok);
            menu.add(0, 1, 1, R.string.back);
            return true;
        }
    
        /*处理menu的事件*/
        public boolean onOptionsItemSelected(MenuItem item)
        {
            //得到当前选中的MenuItem的ID,
            int item_id = item.getItemId();
    
            switch (item_id)
            {
                case 0:
                case 1:
                    /* 新建一个Intent对象 */
                    Intent intent = new Intent();
                    /* 指定intent要启动的类 */
                    intent.setClass(Activity02.this, Activity01.class);
                    /* 启动一个新的Activity */
                    startActivity(intent);
                    /* 关闭当前的Activity */
                    Activity02.this.finish();
                    break;
            }
            return true;
        }
    }

    布局

    menu

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/about"
              android:title="关于" />
        <item android:id="@+id/exit"
              android:title="退出" />
    </menu>

    main

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

    main2

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello2"
        >
        </TextView>
    </LinearLayout>

     string

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">主界面,点击关于会跳到另一个界面!(Activity01)</string>
        <string name="hello2">关于
    Android Menu使用范例!(Activity02)</string>
        <string name="app_name">lxt008 Android Demo</string>
        <string name="ok">确定</string>
        <string name="back">返回</string>
    </resources>
  • 相关阅读:
    javascript 事件冒泡
    Java 理论与实践: 正确使用 Volatile 变量
    Concurrency,Java 并发
    POJ2379 ACM Rank Table 模拟题
    HDU1711Number Sequence KMP
    POJ1061 青蛙的约会 扩展GCD
    HDU2523 SORT AGAIN HASH
    HDU2087剪花布条 KMP
    HDU3736 Cyclic Nacklace KMP
    HDU1709The Balance 母函数
  • 原文地址:https://www.cnblogs.com/spadd/p/4189927.html
Copyright © 2011-2022 走看看