MenuComponentActivity.java
import android.app.Activity;
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.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;
/**
* 测试菜单Menu
* @author 悆您
* OptionMenu
* 1.如何触发Menu的显示?
*点击Menu键
*2.如何向Menu中添加MenuItem?
*重写onCreateOptionsMenu方法
*
1.menu.add()
*
2.菜单文件
*3.选择某个MenuItem时如何响应?
*
重写onOptionsItemSelected方法
*
*
*
* 1.如何触发Menu的显示?
*长按某个视图,并View.setOnCreateContextListener(this)
*2.如何向Menu中添加MenuItem?
*重写onCreateContextMenu()方法,用menu.add()添加,(菜单文件方式也可以)
*3.选择某个MenuItem时如何响应?
*重写onContextItemSelected方法,根据ItemId响应
*/
public class MenuComponentActivity extends Activity {
private Button btn_menu_cm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_component);
btn_menu_cm = (Button) findViewById(R.id.btn_menu_cm);
//设置创建上下文菜单的监听
btn_menu_cm.setOnCreateContextMenuListener(this);
}
//用来显示选型菜单的方法,向Menu中添加Item
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//纯编码方式
menu.add(0, 2, 0, "添加");
menu.add(0, 3, 0, "删除");
return super.onCreateOptionsMenu(menu);
}
//
@Override
//
public boolean onCreateOptionsMenu(Menu menu) {
//
//菜单文件的方式
//
//1.得到菜单加载器对象
//
MenuInflater menuInflater = getMenuInflater();
//
//2.加载菜单文件
//
menuInflater.inflate(R.menu.option_menu, menu);
//
return super.onCreateOptionsMenu(menu);
//
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
//
case R.id.add:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 3:
//
case R.id.add:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//添加菜单项
menu.add(0,1,0,"添加");
menu.add(0,2,0,"删除");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
activity_menu_component.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_menu_cm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/menu_btn" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.点击Menu显示选项菜单\n2.长按按钮显示上下文菜单"
android:textSize="30sp"/>
</LinearLayout>
菜单文件方式
option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add"
android:title="添加2"/>
<item
android:id="@+id/del"
android:title="删除2"/>
</menu>