显示上下文菜单

作者:追风剑情 发布于:2015-9-24 22:13 分类:Android

视图

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="@string/hello_world" />
  12. <Button
  13. android:id="@+id/button1"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="Click and hold on it"/>
  17.  
  18. </LinearLayout>

代码

  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.ContextMenu;
  6. import android.view.ContextMenu.ContextMenuInfo;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.Toast;
  12.  
  13. public class ContextMenuActivity extends Activity {
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_context_menu);
  19. Button btn = (Button) findViewById(R.id.button1);
  20. btn.setOnCreateContextMenuListener(this);
  21. }
  22. @Override
  23. public void onCreateContextMenu(ContextMenu menu, View view,
  24. ContextMenuInfo menuInfo)
  25. {
  26. super.onCreateContextMenu(menu, view, menuInfo);
  27. CreateMenu(menu);
  28. }
  29. @Override
  30. public boolean onContextItemSelected(MenuItem item)
  31. {
  32. return MenuChoice(item);
  33. }
  34. private void CreateMenu(Menu menu)
  35. {
  36. menu.setQwertyMode(true);//使菜单项的快捷键生效
  37. MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
  38. {
  39. mnu1.setAlphabeticShortcut('a');
  40. mnu1.setIcon(R.drawable.ic_launcher);
  41. }
  42. MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
  43. {
  44. mnu2.setAlphabeticShortcut('b');
  45. mnu2.setIcon(R.drawable.ic_launcher);
  46. }
  47. MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
  48. {
  49. mnu3.setAlphabeticShortcut('c');
  50. mnu3.setIcon(R.drawable.ic_launcher);
  51. }
  52. MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
  53. {
  54. mnu4.setAlphabeticShortcut('d');
  55. }
  56. menu.add(0, 4, 4, "Item 5");
  57. menu.add(0, 5, 5, "Item 6");
  58. menu.add(0, 6, 6, "Item 7");
  59. }
  60. private boolean MenuChoice(MenuItem item)
  61. {
  62. switch(item.getItemId())
  63. {
  64. case 0:
  65. Toast.makeText(this, "You clicked on Item 1",
  66. Toast.LENGTH_SHORT).show();
  67. return true;
  68. case 1:
  69. Toast.makeText(this, "You clicked on Item 2",
  70. Toast.LENGTH_SHORT).show();
  71. return true;
  72. case 2:
  73. Toast.makeText(this, "You clicked on Item 3",
  74. Toast.LENGTH_SHORT).show();
  75. return true;
  76. case 3:
  77. Toast.makeText(this, "You clicked on Item 4",
  78. Toast.LENGTH_SHORT).show();
  79. return true;
  80. case 4:
  81. Toast.makeText(this, "You clicked on Item 5",
  82. Toast.LENGTH_SHORT).show();
  83. return true;
  84. case 5:
  85. Toast.makeText(this, "You clicked on Item 6",
  86. Toast.LENGTH_SHORT).show();
  87. return true;
  88. case 6:
  89. Toast.makeText(this, "You clicked on Item 7",
  90. Toast.LENGTH_SHORT).show();
  91. return true;
  92. }
  93. return false;
  94. }
  95. }

运行效果

mmmm1.pngm2.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号