Android learning: Use menu

Source: Internet
Author: User

Mainactivity. Java

Package COM. example. examples_04_13; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. menuitem; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true;}/* process menu events */Public Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubint item_id = item. getitemid (); Switch (item_id) {case R. id. about: intent = new intent (); intent. setclass (mainactivity. this, activity2.class); startactivity (intent); mainactivity. this. finish (); break; case R. id. exit: mainactivity. this. finish (); default: break;} return Super. onoptionsitemselected (item );}}

Activity2.java

Package COM. example. examples_04_13; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuitem; public class activity2 extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. layout2);}/* Create menu */Public Boolean oncreateoptionsmenu (menu) {// Add content menu for menu. add (0, 0, 0, R. string. OK); menu. add (0, 1, 1, R. string. back); return Super. oncreateoptionsmenu (menu);}/* process menu events */Public Boolean onoptionsitemselected (menuitem item) {int item_id = item. getitemid (); Switch (item_id) {Case 0: activity2.this. finish (); break; Case 1: intent = new intent (); intent. setclass (activity2.this, mainactivity. class); startactivity (intent); activity2.this. finish (); break; default: break;} return Super. onoptionsitemselected (item );}}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="MainActivity" /></RelativeLayout>

Layout2.xml

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

Res/menu/Main. xml

<Menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: Id = "@ + ID/about" Android: Title = "about"/> <item Android: id = "@ + ID/exit" Android: Title = "quit"/> </menu>

Androidmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.examples_04_13"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.examples_04_13.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity             android:name="com.example.examples_04_13.Activity2">                    </activity>    </application></manifest>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.