代码
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_context);
// 注册上下文菜单
registerForContextMenu(button);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_context);
// 注册上下文菜单
registerForContextMenu(button);
}
// 创建上下文此菜单的方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// 加载菜单的布局并且设置菜单对象
getMenuInflater().inflate(R.menu.mymenu, menu);
}
//上下文菜单项的监听
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_start:
Toast.makeText(MainActivity.this, "游戏已经开始", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_end:
Toast.makeText(MainActivity.this, "结束游戏", Toast.LENGTH_SHORT).show();
break;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_start:
Toast.makeText(MainActivity.this, "游戏已经开始", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_end:
Toast.makeText(MainActivity.this, "结束游戏", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
//c创建选项菜单的方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 100, "开始游戏");
menu.add(0, 1, 101, "结束游戏");
SubMenu stubmenu = menu.addSubMenu(0, 2, 103, "游戏设置");
stubmenu.add(1, 3, 100, "背景设置");
stubmenu.add(1, 4, 100, "音乐设置");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 100, "开始游戏");
menu.add(0, 1, 101, "结束游戏");
SubMenu stubmenu = menu.addSubMenu(0, 2, 103, "游戏设置");
stubmenu.add(1, 3, 100, "背景设置");
stubmenu.add(1, 4, 100, "音乐设置");
return super.onCreateOptionsMenu(menu);
}
//选项菜单项的监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(MainActivity.this, "游戏已经开始", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "结束游戏了", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this, "游戏设置", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this, "背景设置", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, "音乐游戏", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
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="com.example.day05menudemo.MainActivity">
<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="com.example.day05menudemo.MainActivity">
<Button
android:id="@+id/btn_context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出上下文菜单"
/>
</RelativeLayout>
android:id="@+id/btn_context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出上下文菜单"
/>
</RelativeLayout>