菜單(一),菜單(

來源:互聯網
上載者:User

菜單(一),菜單(

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);//載入菜單的定義方式//使用java代碼建立菜單,在Android中最多可以顯示6個菜單//第二個參數為菜單唯一的標識符MenuItem menuItem = menu.add(1001, 100, 1, "系統功能表");//menuItem.setIcon(R.drawable.ic_launcher);//運行後發現表徵圖顯示不出來,在高版本不建議這樣添加表徵圖menuItem.setTitle("菜單一");MenuItem menuItem2 = menu.add(1001, 101, 2, "使用者菜單");menuItem2.setTitle("菜單二");MenuItem menuItem3 = menu.add(1001, 102, 3, "客戶菜單");menuItem3.setTitle("菜單三");//menuItem3.setShortcut('c', 'c');//給指定的菜單設定快速鍵return true;}@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, "選擇了菜單一", Toast.LENGTH_SHORT).show();;break;case 101:intent.putExtra("number", 2);item.setIntent(intent);//Toast.makeText(MainActivity.this, "選擇了菜單二", Toast.LENGTH_SHORT).show();;break;case 102:intent.putExtra("number", 3);item.setIntent(intent);//Toast.makeText(MainActivity.this, "選擇了菜單三", Toast.LENGTH_SHORT).show();break;default:break;}return super.onOptionsItemSelected(item);}}

菜單跳轉的頁面

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("您點擊了菜單" + 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>

原始碼下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.