網站首頁 編程語言 正文
簡介
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
相關推薦
- 2022-05-13 分布式架構Redis中有哪些數據結構及底層實現原理_Redis
- 2022-08-19 利用Python實現簡單的驗證碼處理_python
- 2022-09-14 Redis核心原理詳細解說_Redis
- 2023-03-29 python中文字符如何轉url編碼_python
- 2023-01-07 Android實現簡單的自定義ViewGroup流式布局_Android
- 2022-06-01 React函數式組件與類組件的不同你知道嗎_React
- 2021-12-13 C++??系統IO流介紹_C 語言
- 2023-02-06 C#實現獲取機器碼的示例詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支