日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android入門之Menu組件的使用教程詳解_Android

作者:TGITCIC ? 更新時間: 2022-12-23 編程語言

簡介

Android有不同的菜單:

  • 系統菜單
  • 彈出菜單(可自定義樣式)
  • Context Menu;
  • 子菜單;

菜單的使用和我們前面說的AlertDialog很像。它可以支持自定義樣式、也可以對菜單的點擊事件進行綁定。

Android里有幾個MainActivity事件可以覆蓋,其中有以下幾個事件就是用于處理Menu的。

  • onCreateOptionsMenu(Menu menu),在這個方法里如果你使用menu.add就可以實現在Android的手機里右上角的...圖標點擊出現系統菜單的效果;
  • onOptionsItemSelected(MenuItem item),這個方法里可以進行每個系統菜單項點擊的處理事件;
  • onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) ,這個是用來創建ContextMenu的,即這個菜單可以綁定在一個組件上,比如說按一下一個組件,對于這個組件的菜單就會被彈出。但是它要啟作用必須在程序運行開始時調用一下Activity里的自帶方法:registerForContextMenu(View view);來指定,ContextMenu綁定在哪個組件的身上;
  • onContextItemSelected(MenuItem item),相應的對于ContextMenu的每一個item的點擊處理方法,用戶可以自定義和覆蓋里面的邏輯;

課程目標

  • 做一個可以改變屏幕中間TextView字體顏色的系統菜單;
  • 點一下按鈕彈出一個PopMenu;
  • 對于一個TextView綁定一個ContextMenu;

代碼

前端代碼

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/viewContext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="長按出context menu"
        android:textSize="18sp" />
 
    <TextView
        android:id="@+id/textColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="選擇菜單改變顏色"
        android:textSize="18sp" />
 
 
    <Button
        android:id="@+id/btnShowMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textColor"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="展示彈出菜單" />
 
 
</RelativeLayout>

系統菜單

前端代碼

不需要

后端代碼

MainActivity.java

系統菜單涉及到兩個方法的覆蓋,因此只要在這兩個方法把系統菜單設上,同時對系統菜單的“選擇”事件做出自定義即可,在此我們通過系統菜單改變屏幕中一行字的字體顏色

//1.定義不同顏色的菜單項的標識:
  private final int RED = 101;
  private final int GREEN = 102;
  private final int BLUE = 103;
  private final int YELLOW = 104;
  private final int GRAY = 105;
  private final int CYAN = 106;
  private final int BLACK = 107;
@Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     menu.add(1, RED, 4, "紅色");
     menu.add(1, GREEN, 2, "綠色");
     menu.add(1, BLUE, 3, "藍色");
     menu.add(1, YELLOW, 1, "黃色");
     menu.add(1, GRAY, 5, "灰色");
     menu.add(1, CYAN, 6, "藍綠色");
     menu.add(1, BLACK, 7, "黑色");
     return true;
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     int id = item.getItemId();
     switch (id) {
         case RED:
             textColor.setTextColor(Color.RED);
             break;
         case GREEN:
             textColor.setTextColor(Color.GREEN);
             break;
         case BLUE:
             textColor.setTextColor(Color.BLUE);
             break;
         case YELLOW:
             textColor.setTextColor(Color.YELLOW);
             break;
         case GRAY:
             textColor.setTextColor(Color.GRAY);
             break;
         case CYAN:
             textColor.setTextColor(Color.CYAN);
             break;
         case BLACK:
             textColor.setTextColor(Color.BLACK);
             break;
     }
     return super.onOptionsItemSelected(item);
 }

彈出菜單

前端代碼

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/mcat" android:title="一只喵" />
    <item android:id="@+id/mdog" android:title="一只汪" />
</menu>

后端代碼

   btnShowMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(MainActivity.this,btnShowMenu);
            popup.getMenuInflater().inflate(R.menu.pop_menu, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()){
                        case R.id.mcat:
                            Toast.makeText(MainActivity.this,"你輕拍了一下喵",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.mdog:
                            Toast.makeText(MainActivity.this,"你輕拍了一下汪",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                    return true;
                }
            });
            popup.show();
        }
    });
}

ContextMenu

前端

context_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="none">
        <item android:id="@+id/blue" android:title="@string/font_blue"/>
        <item android:id="@+id/green" android:title="@string/font_green"/>
        <item android:id="@+id/red" android:title="@string/font_red"/>
    </group>
</menu>

后端

我們可以看到這個ContextMenu是綁定在一個TextView上的。

viewContext=(TextView)findViewById(R.id.viewContext);
registerForContextMenu(viewContext);
 
   //重寫上下文菜單的創建方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflator = new MenuInflater(this);
        inflator.inflate(R.menu.context_menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    //上下文菜單被點擊是觸發該方法
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.blue:
                viewContext.setTextColor(Color.BLUE);
                break;
            case R.id.green:
                viewContext.setTextColor(Color.GREEN);
                break;
            case R.id.red:
                viewContext.setTextColor(Color.RED);
                break;
        }
        return true;
    }
}

原文鏈接:https://blog.csdn.net/lifetragedy/article/details/128028803

欄目分類
最近更新