Menu (1), menu (

Source: Internet
Author: User

Menu (1), menu (

Package com. example. menutest; import android. app. activity; import android. content. intent; import android. OS. bundle; 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 menu) {// getMenuInflater (). inflate (R. menu. main, menu); // load the menu definition method // use java code to create a menu, in Android, a maximum of six menus can be displayed. // The second parameter is the unique menu identifier MenuItem menuItem = menu. add (1001,100, 1, "System Menu"); // menuItem. setIcon (R. drawable. ic_launcher); // after running, the icon cannot be displayed. It is not recommended to add the icon menuItem in the later version. setTitle ("menu 1"); MenuItem menuItem2 = menu. add (1001,101, 2, "user menu"); menuItem2.setTitle ("menu 2"); MenuItem menuItem3 = menu. add (1001,102, 3, "customer menu"); menuItem3.setTitle ("menu 3"); // menuItem3.setShortcut ('C', 'C '); // set the shortcut key return true for the specified menu;} @ Overridepublic boolean onOptionsItemSelected (MenuItem item) {Intent intent = new Intent (); intent. setClass (MainActivity. this, TestActivity. class); switch (item. getItemId () {case 100: intent. putExtra ("number", 1); item. setIntent (intent); // Toast. makeText (MainActivity. this, "Select menu 1", Toast. LENGTH_SHORT ). show (); break; case 101: intent. putExtra ("number", 2); item. setIntent (intent); // Toast. makeText (MainActivity. this, "menu 2 selected", Toast. LENGTH_SHORT ). show (); break; case 102: intent. putExtra ("number", 3); item. setIntent (intent); // Toast. makeText (MainActivity. this, "menu 3 selected", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item );}}

Menu jump page

Package com. example. menutest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class TestActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. test); TextView textView = (TextView) findViewById (R. id. textView1); Intent intent = getIntent (); int number = intent. getIntExtra ("number", 0); textView. setText ("You have clicked the menu" + number );}}
<?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:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextActivity"         android:textSize="30sp"/></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.menutest"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".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.menutest.TestActivity"></activity>    </application></manifest>

Source code download

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.