網站首頁 編程語言 正文
本文實例為大家分享了Android學習之菜單使用的具體代碼,供大家參考,具體內容如下
Android中菜單包含上下文菜單和選項菜單兩種類型。
使用統一的菜單類來管理菜單:
Menu、MenuItem、SubMenu三個類。
1.Menu類:
一個Menu對象代表一個菜單,是整個菜單的基礎。
Menu類的常用方法:添加一個菜單:add(int groupId, int itemId, int order, CharSequence title)
2.MenuItem類:
MenuItem代表一個菜單項;
MenuItem往往是通過Menu的add方法來獲取的。
3.subMenu類:
subMenu繼承自Menu,其代表一個子菜單。
Menu、MenuItem、SubMenu的使用:
Menu通過add方法,來添加菜單項MenuItem并返回;
Menu通過addSubMenu方法,來添加自菜單SubMenu并返回;
通過MenuItem的多個方法,來設定菜單項的狀態;
SubMenu繼承自Menu,用相同方法,添加MenuItem及SubMenu;
通過SubMenu的多個方法,設定SubMenu的狀態。
1.ContextMenu(上下文菜單):
ContextMenu(上下文菜單)類似于PC中的右鍵彈出菜單,當一個視圖注冊了上下文菜單時,長按該視圖對象將出現一個提供相關功能的浮動菜單。上下文菜單可以被注冊到任何視圖對象中,最常見的是用于列表視圖ListView中,但上下文菜單不支持圖標和快捷鍵。
使用步驟如下:
(1)在Activity中使用上下文菜單,復寫onCreateContextMenu() 和 onContextItemSelected()方法;
(2)為視圖控件View中注冊上下文菜單,使用registerForContextMenu()方法;
(3)在onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info)中添加菜單項menu.add();
(4)在onContextItemSelected()中通過設置菜單的id來實現菜單子項的監聽。
ContextMenu在Activity中的回調函數:
onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info)
//初始化,第一次調用菜單時執行,用于創建菜單項目
onContextItemSelected(MenuItem item)
//菜單項被選中后調用,用于處理菜單事件,根據傳遞的item參數,確定具體的菜單項,進行設計
onContextMenuClosed(Menu menu) //菜單被關閉時調用
``
2.OptionsMenu(選項菜單):
使用步驟如下:
(1)在Activity中復寫onCreateOptionsMenu()和onOptionsItemSelected()方法;
(2)在onCreateOptionsMenu(Menu menu)中添加菜單選項,有兩種方式:
a、調用menu.add()
b、從布局文件中添加,在res下新建menu文件夾,創建menu文件,填寫相應的選項;
(2)為視圖控件View中注冊上下文菜單,使用registerForContextMenu()方法;
OptionsMenu選項菜單在Activity中的回調方法:
onCreateOptionsMenu(Menu menu)?
//初始化選項菜單,初次顯示菜單時調用
public boolean onOptionsItemSeleted(MenuItem item)
//選項菜單中的某個選項被選中時,調用該方法
public void onOptionMenuClosed(Menu menu)
//當關閉菜單時調用該方法
☆☆☆Android Studio實現使用菜單的使用
1.打開Android Studio,新建工程后,在activity_main.xml中,編寫代碼。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/activity_main" ? ? 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" ? ? tools:context="com.example.androidlesson.MainActivity" ? ? android:background="@android:color/holo_blue_dark"> ? ?? ? ? <TextView ? ? ? ? android:text="TextView" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignParentTop="true" ? ? ? ? android:layout_alignParentStart="true" ? ? ? ? android:id="@+id/textView" ? ? ? ? android:background="@android:color/holo_green_dark" ? ? ? ? android:textSize="36sp" /> ? ? ? ?? ? ? <ImageView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? app:srcCompat="@mipmap/ic_launcher" ? ? ? ? android:id="@+id/imageView" ? ? ? ? android:background="@android:color/holo_purple" ? ? ? ? android:layout_below="@+id/textView" ? ? ? ? android:layout_alignParentStart="true" ? ? ? ? android:layout_marginTop="14dp" /> ? ? ? ?? </RelativeLayout>
2.在MainActivity.java中,編寫相關代碼。
package com.example.menu;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
? ? TextView tv;
? ? ImageView iv;
? ??
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? tv= (TextView) findViewById(R.id.textView);
? ? ? ? iv= (ImageView) findViewById(R.id.imageView);
? ? ? ? registerForContextMenu(tv);
? ? ? ? registerForContextMenu(iv);
? ? }
? ??
? ? @Override
? ? public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
? ? ? ? if (v.getId()==tv.getId()) {
? ? ? ? ? ? //menu.add(0,0,0,"打開");
? ? ? ? ? ? menu.add(0, 0, 0, "大號字體");
? ? ? ? ? ? //menu.add(0,1,1,"保存");
? ? ? ? ? ? menu.add(0, 1, 1, "小號字體");
? ? ? ? ? ? //SubMenu sm1=menu.addSubMenu(0,2,2,"編輯");
? ? ? ? ? ? SubMenu sm1 = menu.addSubMenu(0, 2, 2, "顏色");
? ? ? ? ? ? //sm1.add(0,3,3,"復制");
? ? ? ? ? ? //sm1.add(0,4,4,"粘貼");
? ? ? ? ? ? sm1.add(0, 3, 3, "黑色");
? ? ? ? ? ? sm1.add(0, 4, 4, "紅色");
? ? ? ? }else {
? ? ? ? ? ? menu.add(1,5,5,"圖片信息");
? ? ? ? ? ? menu.add(1,6,6,"圖片說明");
? ? ? ? }
? ? ? ? super.onCreateContextMenu(menu, v, menuInfo);
? ? }
? ??
? ? @Override
? ? public boolean onContextItemSelected(MenuItem item) {
? ? ? ? switch (item.getItemId()){
? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? tv.setTextSize(32);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? tv.setTextSize(16);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? tv.setTextColor(Color.BLACK);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? tv.setTextColor(Color.RED);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? Toast.makeText(this,"png格式圖片,分辨率64",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? Toast.makeText(this,"這是安卓機器人的圖片",Toast.LENGTH_LONG).show();
? ? ? ? }
? ? ? ? return super.onContextItemSelected(item);
? ? }
? ??
? ? @Override
? ? public boolean onCreateOptionsMenu(Menu menu) {
? ? ? ? SubMenu sub1=menu.addSubMenu(0,0,0,"文件");
? ? ? ? SubMenu sub2=menu.addSubMenu(0,1,1,"編輯");
? ? ? ? sub1.add(0,2,2,"打開");
? ? ? ? sub1.add(0,3,3,"保存");
? ? ? ? sub1.add(0,4,4,"另存為");
? ? ? ? sub2.add(0,5,5,"復制");
? ? ? ? sub2.add(0,6,6,"粘貼");
? ? ? ? menu.add(0,7,7,"退出");
? ? ? ? return super.onCreateOptionsMenu(menu);
? ? }
? ??
? ? @Override
? ? public boolean onOptionsItemSelected(MenuItem item) {
? ? ? ? switch (item.getItemId()){
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? finish();
? ? ? ? }
? ? ? ? return super.onOptionsItemSelected(item);
? ? }
}
運行結果:
這就是菜單的簡單使用。
原文鏈接:https://blog.csdn.net/weixin_43468667/article/details/96154042
相關推薦
- 2022-08-25 .NET6環境下實現MQTT通信及詳細代碼演示_實用技巧
- 2022-11-08 background-image 背景平鋪方式、 CSS3 background-size背景圖像大
- 2022-04-25 Pycharm下載pyinstaller報錯:You?should?consider?upgradi
- 2022-07-17 SQL?Server格式轉換函數Cast、Convert介紹_MsSql
- 2022-03-03 記一次瀏覽器SameSite策略更新,導致接口 Failed to load response da
- 2022-03-28 c語言for、while和do-while循環之間的區別_C 語言
- 2022-02-13 QT 控件 QListWidget 設置所有事件都激發編輯(雙擊, 選擇, 選項變化)
- 2022-03-22 C++this指針詳情_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同步修改后的遠程分支