android 实现点击手机的MENU键,弹出选项菜单,其中菜单项有:科目、爱好。点击科目弹出Java语言、Python语言、Android。点击爱好菜单项则弹出子菜单,分别为运动、学习、看书和爬山。点击每一个子菜单项,用toast提示菜单项的内容。
时间: 2024-05-19 10:16:11 浏览: 105
基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出
首先,在MainActivity.java中实现以下代码:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.subject_menu:
showSubjectMenu();
return true;
case R.id.hobby_menu:
showHobbyMenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// 显示科目菜单
private void showSubjectMenu() {
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.subject_menu));
popupMenu.getMenuInflater().inflate(R.menu.subject_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.java_subject:
Toast.makeText(MainActivity.this, "Java语言", Toast.LENGTH_SHORT).show();
return true;
case R.id.python_subject:
Toast.makeText(MainActivity.this, "Python语言", Toast.LENGTH_SHORT).show();
return true;
case R.id.android_subject:
Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
popupMenu.show();
}
// 显示爱好菜单
private void showHobbyMenu() {
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.hobby_menu));
popupMenu.getMenuInflater().inflate(R.menu.hobby_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.sports_hobby:
Toast.makeText(MainActivity.this, "运动", Toast.LENGTH_SHORT).show();
return true;
case R.id.study_hobby:
Toast.makeText(MainActivity.this, "学习", Toast.LENGTH_SHORT).show();
return true;
case R.id.read_hobby:
Toast.makeText(MainActivity.this, "看书", Toast.LENGTH_SHORT).show();
return true;
case R.id.climb_hobby:
Toast.makeText(MainActivity.this, "爬山", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
popupMenu.show();
}
}
```
其中,onCreateOptionsMenu方法用于加载菜单布局文件,onOptionsItemSelected方法用于处理菜单项点击事件。showSubjectMenu方法和showHobbyMenu方法分别用于弹出科目菜单和爱好菜单,并且为每个菜单项设置点击事件,使用Toast提示菜单项的内容。
接下来,在res/menu文件夹下,创建以下三个菜单布局文件:
1. main_menu.xml(主菜单)
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/subject_menu"
android:title="科目"
android:icon="@drawable/ic_menu_subject"
android:showAsAction="always" />
<item
android:id="@+id/hobby_menu"
android:title="爱好"
android:icon="@drawable/ic_menu_hobby"
android:showAsAction="always" />
</menu>
```
2. subject_menu.xml(科目菜单)
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/java_subject"
android:title="Java语言"
android:icon="@drawable/ic_menu_java" />
<item
android:id="@+id/python_subject"
android:title="Python语言"
android:icon="@drawable/ic_menu_python" />
<item
android:id="@+id/android_subject"
android:title="Android"
android:icon="@drawable/ic_menu_android" />
</menu>
```
3. hobby_menu.xml(爱好菜单)
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/sports_hobby"
android:title="运动"
android:icon="@drawable/ic_menu_sports" />
<item
android:id="@+id/study_hobby"
android:title="学习"
android:icon="@drawable/ic_menu_study" />
<item
android:id="@+id/read_hobby"
android:title="看书"
android:icon="@drawable/ic_menu_read" />
<item
android:id="@+id/climb_hobby"
android:title="爬山"
android:icon="@drawable/ic_menu_climb" />
</menu>
```
其中,ic_menu_subject、ic_menu_hobby、ic_menu_java、ic_menu_python、ic_menu_android、ic_menu_sports、ic_menu_study、ic_menu_read、ic_menu_climb是自定义的菜单图标,可以根据实际情况进行修改。
最终实现效果如下图所示:
![menu_demo](https://user-images.githubusercontent.com/1649961/132389334-0b5e6c0f-1b8d-4d1c-9e64-268b0e81f3cf.gif)
阅读全文