选项菜单-上下文菜单
菜单
分类
选项菜单 OptionMenu
按menu键触发
事件源是Activity
上下文菜单 ContextMenu
长按组件时触发
子菜单 SubMenu
addSubMenu( )添加子菜单
用法
选项菜单
创建:重写onCreatOptionsMenu(Menu menu)
在代码里给menu添加MenuItem
纯java代码
menu.add( )
加载menu资源文件
1-获得Menu加载器
MenuInflater m=getMenuInflater( );
2-加载Menu资源文件
m.inflat(Menu资源文件的id,menu当前菜单)
以上两种加载方式可以并存
回调:重写boolean onOptionsItemSelected(MenuItem item)
上下文菜单
Activity 是OnCreateContextMenuListener的实现类
创建:组件.setOnCreateContextMenuListener(this)
在代码里添加MenuItem,方式同 选项菜单
响应:重写Activity的boolean onContextItemSelected(MenuItem item)
与LongClick监听器的关系
LongClick监听器的优先级高
boolean onLongClick(View v)的返回值决定后续的监听器能否被处理
package com.hanqi.testapp2; 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.SubMenu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class TestActivity3 extends Activity { Button bt_1; Button bt_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test3); bt_1=(Button)findViewById(R.id.bt_1); bt_2=(Button)findViewById(R.id.bt_2); //给组件添加上下菜单 Activity是OnCreateContextMenuListener的实现类 bt_2.setOnCreateContextMenuListener( this ); //添加长按监听器 // bt_2.setOnLongClickListener(new View.OnLongClickListener() { // @Override // public boolean onLongClick(View v) { // // Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show(); // // return true;//后面不再处理 // } // }); //点击 bt_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show(); } }); //长按 bt_1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show(); //事件是否已经被消费 return true; } }); } //重写创建上下文菜单的方法 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //java menu.add(1,0,1,"字体大小"); menu.add(1, 1, 2, "背景颜色"); SubMenu sm=menu.addSubMenu(1, 2, 3, "子菜单"); sm.add(2,21,0,"子菜单1"); sm.add(2,22,1,"子菜单2"); //加载文件 MenuInflater m=getMenuInflater(); m.inflate(R.menu.test3_menu,menu); } @Override public boolean onContextItemSelected(MenuItem item) { Toast.makeText(TestActivity3.this, "上下文菜单的标题title="+item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); } //给activity 添加选项菜单 //重写 @Override public boolean onCreateOptionsMenu(Menu menu) { //添加菜单 // menu.add(1,1,0,"添加"); // menu.add(1,2,2,"修改"); // menu.add(1,3,1,"删除"); //加载菜单文件方式 //1-获得菜单加载器 MenuInflater m=getMenuInflater(); //2-加载菜单文件 1)菜单文件 2)当前菜单 m.inflate(R.menu.test3_menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(TestActivity3.this, "菜单项的id="+item.getItemId()+"菜单项的title"+item.getTitle(), Toast.LENGTH_SHORT).show(); //怎么判断菜单文件里的菜单哪个被选中了 if(item.getItemId()==R.id.me_1) { Toast.makeText(TestActivity3.this, "执行删除动作", Toast.LENGTH_SHORT).show(); } else if (item.getItemId()==R.id.me_2) { Toast.makeText(TestActivity3.this, "执行修改动作", Toast.LENGTH_SHORT).show(); } return super.onOptionsItemSelected(item); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hanqi.testapp2.TestActivity3" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拨打电话" android:id="@+id/bt_1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上下文菜单按钮" android:id="@+id/bt_2" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/me_1" android:title="删除"/> <item android:id="@+id/me_2" android:title="修改"/> </menu>