android 快顯功能表,android快顯功能表

來源:互聯網
上載者:User

android 快顯功能表,android快顯功能表

<!-- 定義基礎布局LinearLayout --><LinearLayout 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"    android:orientation="vertical">    <!-- 定義TextView控制項 -->    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="請按鍵盤物理菜單鍵,彈出選項菜單"        /></LinearLayout>
package com.example.yanlei.yl2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;//匯入必備的包public class MainActivity extends AppCompatActivity {    //定義Menu中每個菜單選項的Id    private final static int Menu_1 = Menu.FIRST;    private final static int Menu_2 = Menu.FIRST + 1;    private final static int Menu_3 = Menu.FIRST + 2;    private final static int Menu_4 = Menu.FIRST + 3;    private final static int Menu_5 = Menu.FIRST + 4;    private final static int Menu_6 = Menu.FIRST + 5;    private final static int Menu_7 = Menu.FIRST + 6;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState); // 調用父類的onCreate方法        // 通過setContentView方法設定當前頁面的布局檔案為activity_main        setContentView(R.layout.activity_main);    }    //建立Menu菜單的回調方法    public boolean onCreateOptionsMenu(Menu m) {        //參數m就是拿到的當前Activity菜單對象        //想要給當前頁面添加方法的話就add進去即可        //add方法的參數:add(分組id,itemid, 排序, 菜單文字)        m.add(0, Menu_1, 0, "編輯模式");        m.add(0, Menu_2, 0, "修改壁紙");        m.add(0, Menu_3, 0, "全域搜尋");        m.add(0, Menu_4, 0, "案頭縮圖");        m.add(0, Menu_5, 0, "案頭效果");        m.add(0, Menu_6, 0, "系統設定");        m.add(0, Menu_7, 0, "使用者資訊");        return super.onCreateOptionsMenu(m);    }    //Menu菜單選項的選項選擇的回調事件    public boolean onOptionsItemSelected(MenuItem item) {        //參數為使用者選擇的菜單選項對象        //根據菜單選項的id來執行相應的功能        switch (item.getItemId()) {            case 1:                Toast.makeText(this, "你點擊了編輯模式選項", Toast.LENGTH_SHORT).show();                break;            case 2:                Toast.makeText(this, "你點擊了修改壁紙", Toast.LENGTH_SHORT).show();                break;            case 3:                Toast.makeText(this, "你點擊了全域搜尋", Toast.LENGTH_SHORT).show();                break;            case 4:                Toast.makeText(this, "你點擊了案頭縮圖", Toast.LENGTH_SHORT).show();                break;            case 5:                Toast.makeText(this, "你點擊了案頭效果", Toast.LENGTH_SHORT).show();                break;            case 6:                Toast.makeText(this, "你點擊了系統設定", Toast.LENGTH_SHORT).show();                break;            case 7:                Toast.makeText(this, "你點擊了使用者資訊", Toast.LENGTH_SHORT).show();                break;        }        return super.onOptionsItemSelected(item);    }    // 選項菜單關閉時的回調方法    public void onOptionsMenuClosed(Menu menu) {        Log.e("onOptionsMenuClosed","使用者菜單關閉了");    }    // 菜單顯示之前的回調方法    public boolean onPrepareOptionsMenu(Menu menu) {        Log.e("onPrepareOptionsMenu","使用者菜單準備好被顯示了");        //方法返回true,就會顯示Menu,否則Menu不會被顯示        return true;    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.