生活随笔
收集整理的這篇文章主要介紹了
补19
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 創(chuàng)建安卓應(yīng)用
- 將圖片素材導(dǎo)入drawable
- 編寫(xiě)主布局文件activity_main.xml
- 創(chuàng)建上下文菜單ContextMenuDemo.java
- 運(yùn)行效果
創(chuàng)建安卓應(yīng)用
將圖片素材導(dǎo)入drawable
編寫(xiě)主布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"android:gravity="center"tools:context=".MainActivity"><Buttonandroid:id="@+id/btnPopupOptionsMenu"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doPopupOptionsMenu"android:text="@string/popup_options_menu"/></LinearLayout>
創(chuàng)建上下文菜單ContextMenuDemo.java
package net.zjs.options_menu_demo;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/*
* 功能:上下文菜單
* */public class ContextMenuDemo extends AppCompatActivity {private TextView tvFile;private TextView tvEdit;private static final int NEW_MENU_ITEM=1;private static final int OPEN_MENU_ItEM=2;private static final int SAVE_MENU_item=3;private static final int EXIT_MENU_item=4;private static final int CUT_MENU_item=5;private static final int COPY_MENU_item=6;private static final int PASTE_MENU_item=7;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//利用布局資源文件設(shè)置用戶(hù)界面setContentView(R.layout.activity_context_menu_demo);//通過(guò)資源標(biāo)識(shí)符獲取控件實(shí)例tvFile=findViewById(R.id.tvFile);tvEdit=findViewById(R.id.tvEdit);//給控件注冊(cè)上下文菜單registerForContextMenu(tvFile);registerForContextMenu(tvEdit);}/** 設(shè)置圖標(biāo)可用的方法* */public void setIconEnabled(Menu menu,boolean enabled){try{Class<?> clazz=Class.forName("com.android.internal.view.menu.MenuBuilder");Method m=clazz.getDeclaredMethod("setOptionalIconsVisible",boolean.class);m.setAccessible(true);//MenuBuilder實(shí)現(xiàn)Menu接口,創(chuàng)建菜單時(shí),傳進(jìn)來(lái)的menu其實(shí)就是MenuBuilder對(duì)象(java的多態(tài)特征);m.invoke(menu,enabled);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}/** 創(chuàng)建上下文菜單** */@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo){super.onCreateContextMenu(menu,v,menuInfo);//設(shè)置菜單圖標(biāo)可用setIconEnabled(menu,true);//針對(duì)不同控件創(chuàng)建不同的上文菜單switch (v.getId()){case R.id.tvFile://創(chuàng)建文件菜單menu.setHeaderIcon(R.drawable.file);menu.setHeaderTitle("文件");//添加菜單項(xiàng)menu.add(1,NEW_MENU_ITEM,1,"新建").setIcon(R.drawable.new_file);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)menu.add(1,OPEN_MENU_ItEM,2,"打開(kāi)").setIcon(R.drawable.open_file);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)menu.add(1,SAVE_MENU_item,3,"保存").setIcon(R.drawable.save_file);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)menu.add(1,EXIT_MENU_item,4,"退出").setIcon(R.drawable.exit);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)break;case R.id.tvEdit://創(chuàng)建編輯菜單menu.setHeaderIcon(R.drawable.edit);menu.setHeaderTitle("編輯");//添加菜單項(xiàng)menu.add(2,CUT_MENU_item,1,"剪切").setIcon(R.drawable.cut);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)menu.add(2,COPY_MENU_item,2,"復(fù)制").setIcon(R.drawable.copy);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)menu.add(2,PASTE_MENU_item,3,"粘貼").setIcon(R.drawable.paste);//(組編號(hào),菜單項(xiàng)標(biāo)識(shí),菜單項(xiàng)序號(hào),菜單項(xiàng)文本)break;}}/** 上下文菜單項(xiàng)選擇事件處理方法** */@Overridepublic boolean onContextItemSelected(@NonNull MenuItem item){switch (item.getItemId()){case NEW_MENU_ITEM:Toast.makeText(this,"你單擊了【新建】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case OPEN_MENU_ItEM:Toast.makeText(this,"你單擊了【打開(kāi)】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case SAVE_MENU_item:Toast.makeText(this,"你單擊了【保存】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case EXIT_MENU_item:Toast.makeText(this,"你單擊了【退出】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case CUT_MENU_item:Toast.makeText(this,"你單擊了【剪切】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case COPY_MENU_item:Toast.makeText(this,"你單擊了【復(fù)制】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;case PASTE_MENU_item:Toast.makeText(this,"你單擊了【粘貼】菜單項(xiàng)!", Toast.LENGTH_SHORT).show();break;}return true;}}```# 編寫(xiě)activity_context_menu_demo.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="#eeeeee"><TextViewandroid:id="@+id/tvFile"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="15dp"android:textColor="#0000ff"android:text="@string/file"android:textSize="20sp"/><TextViewandroid:id="@+id/tvEdit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="15dp"android:textColor="#0000ff"android:text="@string/edit"android:textSize="20sp"/>
</LinearLayout>
``` # 編寫(xiě)主界面類(lèi)
package net.zjs.options_menu_demo;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.widget.Toolbar;public class MainActivity extends AppCompatActivity {private static final int NEW_MENU=1;private static final int OPEN_MENU=2;private static final int SAVE_MENU=3;private static final int EXIT_MENU=4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/** 創(chuàng)建選項(xiàng)菜單** menu是否成功* */@Overridepublic boolean onCreateOptionsMenu(Menu menu){//添加四個(gè)菜單項(xiàng)menu.add(0,NEW_MENU,1,"新建文件");menu.add(0,OPEN_MENU,2,"打開(kāi)文件");menu.add(0,SAVE_MENU,3,"保存文件");menu.add(0,EXIT_MENU,4,"退出程序");return true;}/*菜單項(xiàng)選擇事件處理方法*/@Overridepublic boolean onOptionsItemSelected(@NonNull MenuItem item){//判斷用戶(hù)單擊了哪個(gè)菜單項(xiàng)switch (item.getItemId()){case NEW_MENU:Toast.makeText(this,"你單擊了【新建文件夾】菜單項(xiàng)!",Toast.LENGTH_SHORT).show();break;case OPEN_MENU:Toast.makeText(this,"你單擊了【打開(kāi)文件夾】菜單項(xiàng)!",Toast.LENGTH_SHORT).show();break;case SAVE_MENU:Toast.makeText(this,"你單擊了【保存文件夾】菜單項(xiàng)!",Toast.LENGTH_SHORT).show();break;case EXIT_MENU:finish();//關(guān)閉當(dāng)前活動(dòng)窗口break;}return true;}/** 彈出選項(xiàng)菜單按鈕單擊事件處理方法*/public void doPopupOptionsMenu(View view){openOptionsMenu();//打開(kāi)選項(xiàng)菜單}//@SuppressLint("NewApi")@Overridepublic void openOptionsMenu(){final View toolbar=getWindow().getDecorView().findViewById(R.id.action_bar);if(toolbar instanceof Toolbar){((Toolbar)toolbar).showOverflowMenu();}else{super.openOptionsMenu();}}}
運(yùn)行效果
總結(jié)
以上是生活随笔為你收集整理的补19的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。