activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_contextmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:gravity="center"
android:text="触发上下文菜单"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/bt_contextview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt_contextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="弹出菜单"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_contextmenu" />
</androidx.constraintlayout.widget.ConstraintLayout>
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="扫一扫" android:icon="@drawable/ic_launcher_background"/>
<item android:title="看一看" />
<item android:title="朋友圈" >
<menu >
<group android:checkableBehavior="single">
<item android:title="B" android:icon="@drawable/ic_launcher_background"/>
<item android:title="A " />
</group>
</menu>
</item>
</menu>
options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="all">
<item android:title="文件(F)"
android:checkable="false">
<menu>
<group>
<item
android:id="@+id/new_file"
android:icon="@drawable/ic_launcher_background"
android:title="新建"/>
<item
android:id="@+id/open_file"
android:icon="@drawable/ic_launcher_background"
android:title="打开"/>
<item
android:id="@+id/save_file"
android:title="保存">
</item>
<item
android:id="@+id/rename_file"
android:title="重命名"/>
</group>
</menu>
</item>
<item
android:checkable="false"
app:showAsAction="never"
android:title="编辑(E)">
<menu>
<item
android:id="@+id/v_file"
android:title="粘贴"/>
<item
android:id="@+id/c_edit"
android:title="复制"/>
<item
android:id="@+id/x_edit"
android:title="剪切"/>
<item
android:id="@+id/rename_edit"
android:title="重命名"/>
</menu>
</item>
<item
android:title="Search"
android:icon="@drawable/ic_launcher_background"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
/>
<item android:title="视图"
android:checkable="true"
android:id="@+id/shitu"/>
<item android:title="导航"/>
<item android:title="代码"/>
<item android:title="分析"/>
<item android:title="反射"/>
</group>
</menu>
MainActivity.java
package com.jld.androidmenus;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
PopupMenu popupMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) this.findViewById(R.id.tv_contextmenu);
registerForContextMenu(tv);
final Button button = (Button) this.findViewById(R.id.bt_contextview);
/* button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
PopupMenu popupMenu = new PopupMenu(MenuActivity.this, v, Gravity.TOP);
*//* Menu menu = popupMenu.getMenu();
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
popupMenu.show();*//*
}
return true;
}
});*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupMenu = new PopupMenu(MainActivity.this, button);
Menu menu = popupMenu.getMenu();
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
popupMenu.show();
/* popupMenu=new PopupMenu(MenuActivity.this,tv);
popupMenu.setGravity(Gravity.LEFT);
Menu menu=popupMenu.getMenu();
menu.add("苹果").setOnMenuItemClickListener(null);
menu.add("黄瓜").setOnMenuItemClickListener(null);
menu.add("苹果").setOnMenuItemClickListener(null);
menu.add("黄瓜").setOnMenuItemClickListener(null);
popupMenu.show();*/
}
});
}
//创建OptionsMenu
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//使用XML方式创建选项菜单
MenuInflater menuInflater = this.getMenuInflater();
menuInflater.inflate(R.menu.options_menu, menu);
System.out.println("88888");
//使用Java代码方式创建选项菜单
/* menu.add("文件");
menu.add("新建").setCheckable(true);
menu.add("打开");
menu.add("编辑");
menu.add("视图");*/
//menu.setGroupCheckable(groupID,false,false);
/* SubMenu fontSubMenu= menu.addSubMenu("字体");
SubMenu fontSize=fontSubMenu.addSubMenu("字体大小");
fontSize.add(0,0,1,"10号字体").setIcon(R.drawable.email);
fontSize.add(0,1,2,"11号字体");
MenuItem menuItem = fontSize.add(0, 2, 0, "12号字体").setIntent(new Intent(this,UserActivity.class ));
fontSize.setGroupCheckable(0,false,false);
SubMenu fontType=fontSubMenu.addSubMenu("字体类型");
fontType.add(1,0,0,"宋体").setIcon(R.drawable.address);
fontType.add(1,1,1,"楷体");
fontType.add(1,2,2,"隶书");
fontType.setGroupCheckable(1,true,false);*/
return true;
}
//选项菜单事件处理方法
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Toast.makeText(this, "groupID:" + item.getGroupId() +
":" + item.getTitle().toString(), Toast.LENGTH_LONG).show();
//!!!!!!!!!!!!!!!!!!!!!!!!!!!选中菜单项
if (item.isCheckable()) {
item.setChecked(true);
System.out.println("test");
}
/* switch (item.getTitle().toString().trim()){
case "文件":
Intent intent=new Intent(this,DialogActivity.class);
this.startActivity(intent);
break;
case "Edit": break;
case "View": break;
}*/
if (item.getTitle().toString().equals("12号字体")) {
//item.setChecked(true);
startActivity(item.getIntent());
}
return true;
}
//重写创建上下文菜单方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
/* menu.setHeaderTitle("国籍上下文菜单");
menu.add("China");
menu.add("America");
menu.add("Russia");*/
}
//重写处理上下文菜单项事件的方法
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast toast = Toast.makeText(this, item.getTitle().toString(), Toast.LENGTH_LONG);
toast.show();
return true;
}
}