layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity3" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="按钮" 17 android:id="@+id/bt_1"/> 18 <Button 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="上下文菜单按钮" 22 android:id="@+id/bt_2"/> 23 </LinearLayout>
java类代码(bt_1代码可不看,bt_2代码为菜单属性相关):
1 package com.hanqi.testapp2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.ContextMenu; 6 import android.view.Menu; 7 import android.view.MenuInflater; 8 import android.view.MenuItem; 9 import android.view.SubMenu; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.Toast; 13 14 public class TestActivity3 extends AppCompatActivity { 15 16 Button bt_1; 17 Button bt_2; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_test3); 22 bt_1 = (Button)findViewById(R.id.bt_1); 23 bt_2 = (Button)findViewById(R.id.bt_2); 24 25 //给组件添加上下文菜单 Activity是OnCreateContextMenuListener的实现类 26 bt_2.setOnCreateContextMenuListener(this); 27 28 //添加长按监听器 29 // bt_2.setOnLongClickListener(new View.OnLongClickListener() { 30 // @Override 31 // public boolean onLongClick(View v) { 32 // Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show(); 33 // return true;//后面不再被处理 34 // } 35 // }); 36 //点击 37 bt_1.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show(); 41 } 42 }); 43 //长按 44 bt_1.setOnLongClickListener(new View.OnLongClickListener() { 45 @Override 46 public boolean onLongClick(View v) { 47 Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show(); 48 //事件是否已被消费 49 return true; 50 } 51 }); 52 53 } 54 55 //重写创建上下文菜单方法 56 @Override 57 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 58 super.onCreateContextMenu(menu, v, menuInfo); 59 //java代码的方式 60 menu.add(1,0,0, "字体大小"); 61 menu.add(1, 1, 1, "背景颜色"); 62 63 //添加子菜单 64 SubMenu sm = menu.addSubMenu(1, 2, 3, "子菜单"); 65 sm.add(2,21,0,"子菜单1"); 66 sm.add(2,22,0,"子菜单2"); 67 //加载文件的方式 68 MenuInflater m = getMenuInflater(); 69 m.inflate(R.menu.test3_menu, menu); 70 } 71 72 @Override 73 public boolean onContextItemSelected(MenuItem item) { 74 Toast.makeText(TestActivity3.this, "上下文菜单的title = "+item.getTitle(), Toast.LENGTH_SHORT).show(); 75 return super.onContextItemSelected(item); 76 } 77 78 //给activity天津选项菜单 79 //重写 80 @Override 81 public boolean onCreateOptionsMenu(Menu menu) { 82 //添加菜单 83 // menu.add(1,1,0,"添加"); 84 // menu.add(1,2,2,"修改"); 85 // menu.add(1,3,1,"删除"); 86 87 //加载菜单文件的方式 88 //1-活动菜单加载器 89 MenuInflater m = getMenuInflater(); 90 //2-加载菜单文件 1)菜单文件 2)当前菜单 91 m.inflate(R.menu.test3_menu,menu); 92 return super.onCreateOptionsMenu(menu); 93 } 94 95 @Override 96 public boolean onOptionsItemSelected(MenuItem item) { 97 // Toast.makeText(TestActivity3.this, "菜单项的id = "+item.getItemId()+ 98 // "菜单项的title = "+item.getTitle(), Toast.LENGTH_SHORT).show(); 99 100 //怎么判断菜单文件里的菜单哪个被选中了 101 if(item.getItemId() == R.id.me_1) 102 { 103 Toast.makeText(TestActivity3.this, "执行删除动作", Toast.LENGTH_SHORT).show(); 104 } 105 else if(item.getItemId() == R.id.me_2) 106 { 107 Toast.makeText(TestActivity3.this, "执行修改动作", Toast.LENGTH_SHORT).show(); 108 } 109 return super.onOptionsItemSelected(item); 110 } 111 }
效果图为:
附相关思维导图: