码迷,mamicode.com
首页 > 其他好文 > 详细

View(视图)———菜单Menu

时间:2016-05-10 02:19:57      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

一.分类

   1.选项菜单  OptionMenu

     1>按menu键触发

     2>事件源时Activity

   2.上下文菜单  ContextMenu

      长按组件时触发。

   3.子菜单 SubMenu

      addSubMenu()添加子菜单。

技术分享
1 <?xml version="1.0" encoding="utf-8"?>
2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item  android:id="@+id/me_1" android:title="删除"/>
5     <item  android:id="@+id/me_2" android:title="修改"/>
6 </menu>
Menu
技术分享
 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.example.wang.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 
19     <Button
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="上下文菜单按钮"
23         android:id="@+id/bt_2"/>
24 
25 </LinearLayout>
SubMenu
技术分享
  1 package com.example.wang.testapp2;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.view.ContextMenu;
  7 import android.view.Menu;
  8 import android.view.MenuInflater;
  9 import android.view.MenuItem;
 10 import android.view.SubMenu;
 11 import android.view.View;
 12 import android.widget.Button;
 13 import android.widget.Toast;
 14 
 15 public class TestActivity3 extends Activity {
 16 
 17     Button bt_1;
 18     Button bt_2;
 19 
 20     @Override
 21     protected void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         setContentView(R.layout.activity_test3);
 24 
 25         bt_1=(Button)findViewById(R.id.bt_1);
 26         bt_2=(Button)findViewById(R.id.bt_2);
 27 
 28         //给组件添加上下文菜单  Activity是OnCreateContextMenuListener的实现类
 29         bt_2.setOnCreateContextMenuListener(
 30                 this
 31         );
 32 
 33         //点击
 34           bt_1.setOnClickListener(new View.OnClickListener() {
 35         @Override
 36         public void onClick(View v) {
 37 
 38             Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
 39         }
 40     });
 41 
 42 
 43     //长按
 44     bt_1.setOnLongClickListener(new View.OnLongClickListener() {
 45         @Override
 46         public boolean onLongClick(View v) {
 47 
 48 
 49             Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
 50 
 51             //事件是否已经被消费
 52             return true;
 53         }
 54     });
 55 }
 56 
 57     //重写创建上下文菜单的方法
 58     @Override
 59     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
 60         super.onCreateContextMenu(menu, v, menuInfo);
 61 
 62         menu.add(1, 0, 1, "字体大小");
 63         menu.add(1,1,2,"背景颜色");
 64 
 65         //添加子菜单
 66         SubMenu sm=menu.addSubMenu(1,2,3,"子菜单");
 67 
 68         sm.add(2, 21, 0, "子菜单1");
 69         sm.add(2, 22, 1,"子菜单2");
 70 
 71         //加载文件
 72         MenuInflater m=getMenuInflater();
 73         m.inflate(R.menu.test3_menu,menu);
 74     }
 75 
 76     @Override
 77     public boolean onContextItemSelected(MenuItem item) {
 78 
 79         Toast.makeText(TestActivity3.this, "上下文菜单的title="+item.getTitle(), Toast.LENGTH_SHORT).show();
 80 
 81         return super.onContextItemSelected(item);
 82     }
 83 
 84     //给Activity 添加选项菜单
 85     //重写
 86     @Override
 87     public boolean onCreateOptionsMenu(Menu menu) {
 88 
 89 
 90         //加载菜单文件方式
 91         //1-获得菜单加载器
 92         MenuInflater m=getMenuInflater();
 93 
 94         //2-加载菜单文件  1)菜单文件 2)当前菜单
 95         m.inflate(R.menu.test3_menu,menu);
 96 
 97 
 98         return super.onCreateOptionsMenu(menu);
 99     }
100 
101 }
SubMenu

技术分享技术分享

二.选项菜单  OptionMenu

   1.创建:重写 boolean  onCreateOptionMenu(Menu menu)

     1>纯Java代码:menu.add();

技术分享
 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.example.wang.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 </LinearLayout>
OptionMenu
技术分享
 1 package com.example.wang.testapp2;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.ContextMenu;
 7 import android.view.Menu;
 8 import android.view.MenuInflater;
 9 import android.view.MenuItem;
10 import android.view.SubMenu;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.Toast;
14 
15 public class TestActivity3 extends AppCompatActivity {
16 
17     Button bt_1;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_test3);
23 
24         bt_1=(Button)findViewById(R.id.bt_1);
25 
26     //点击
27           bt_1.setOnClickListener(new View.OnClickListener() {
28         @Override
29         public void onClick(View v) {
30 
31             Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
32         }
33     });
34 
35 
36     //长按
37     bt_1.setOnLongClickListener(new View.OnLongClickListener() {
38         @Override
39         public boolean onLongClick(View v) {
40 
41 
42             Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
43 
44             //事件是否已经被消费
45             return true;
46         }
47     });
48 }
49 
50       //给Activity 添加选项菜单
51     //重写
52     @Override
53     public boolean onCreateOptionsMenu(Menu menu) {
54 
55         //添加菜单
56         menu.add(1,1,0,"添加");
57         menu.add(1,2,2,"修改");
58         menu.add(1,3,1,"删除");
59 
60         return super.onCreateOptionsMenu(menu);
61     }
62 }
OptionMenu

技术分享

     2>加载menu资源文件

         1-获得Menu加载器: MenuInflater m=getMenuInflater();

         2-加载Menu资源文件:m.inflate(menu资源文件的id,menu当前菜单);

技术分享
 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.example.wang.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 
19 
20 </LinearLayout>
OptionMenu
技术分享
1 <?xml version="1.0" encoding="utf-8"?>
2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item  android:id="@+id/me_1" android:title="删除"/>
5     <item  android:id="@+id/me_2" android:title="修改"/>
6 </menu>
menu
技术分享
 1 package com.example.wang.testapp2;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.ContextMenu;
 7 import android.view.Menu;
 8 import android.view.MenuInflater;
 9 import android.view.MenuItem;
10 import android.view.SubMenu;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.Toast;
14 
15 public class TestActivity3 extends AppCompatActivity {
16 
17     Button bt_1;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_test3);
23 
24         bt_1=(Button)findViewById(R.id.bt_1);
25 
26           //点击
27           bt_1.setOnClickListener(new View.OnClickListener() {
28         @Override
29         public void onClick(View v) {
30 
31             Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
32         }
33     });
34 
35 
36     //长按
37     bt_1.setOnLongClickListener(new View.OnLongClickListener() {
38         @Override
39         public boolean onLongClick(View v) {
40 
41 
42             Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
43 
44             //事件是否已经被消费
45             return true;
46         }
47     });
48 }
49 
50         //给Activity 添加选项菜单
51     //重写
52     @Override
53     public boolean onCreateOptionsMenu(Menu menu) {
54 
55         //加载菜单文件方式
56         //1-获得菜单加载器
57         MenuInflater m=getMenuInflater();
58 
59         //2-加载菜单文件  1)菜单文件 2)当前菜单
60         m.inflate(R.menu.test3_menu,menu);
61 
62 
63         return super.onCreateOptionsMenu(menu);
64     }
65 }
OptionMenu

技术分享

 3>以上两种加载方式可以并存

   2. 回调:重写 boolean onOptionsItemSelected(MenuItem item)

技术分享
 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.example.wang.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 
19 </LinearLayout>
OptionMenu
技术分享
1 <?xml version="1.0" encoding="utf-8"?>
2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
3 
4     <item  android:id="@+id/me_1" android:title="删除"/>
5     <item  android:id="@+id/me_2" android:title="修改"/>
6 </menu>
Menu
技术分享
 1 package com.example.wang.testapp2;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.ContextMenu;
 7 import android.view.Menu;
 8 import android.view.MenuInflater;
 9 import android.view.MenuItem;
10 import android.view.SubMenu;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.Toast;
14 
15 public class TestActivity3 extends AppCompatActivity {
16 
17     Button bt_1;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_test3);
23 
24         bt_1=(Button)findViewById(R.id.bt_1);
25 
26         //点击
27           bt_1.setOnClickListener(new View.OnClickListener() {
28         @Override
29         public void onClick(View v) {
30 
31             Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
32         }
33     });
34 
35 
36     //长按
37     bt_1.setOnLongClickListener(new View.OnLongClickListener() {
38         @Override
39         public boolean onLongClick(View v) {
40 
41 
42             Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
43 
44             //事件是否已经被消费
45             return true;
46         }
47     });
48 }
49 
50     //给Activity 添加选项菜单
51     //重写
52     @Override
53     public boolean onCreateOptionsMenu(Menu menu) {
54 
55         //加载菜单文件方式
56         //1-获得菜单加载器
57         MenuInflater m=getMenuInflater();
58 
59         //2-加载菜单文件  1)菜单文件 2)当前菜单
60         m.inflate(R.menu.test3_menu,menu);
61 
62 
63         return super.onCreateOptionsMenu(menu);
64     }
65 
66     @Override
67     public boolean onOptionsItemSelected(MenuItem item) {
68 
69         Toast.makeText(TestActivity3.this, "菜单项的id="+item.getItemId()+"菜单项的title="+item.getTitle(), Toast.LENGTH_SHORT).show();
70 
71         //怎么判断菜单文件里的菜单哪个被选中了
72         if(item.getItemId()==R.id.me_1)
73         {
74             Toast.makeText(TestActivity3.this, "执行删除动作", Toast.LENGTH_SHORT).show();
75         }
76         else if (item.getItemId()==R.id.me_2)
77         {
78             Toast.makeText(TestActivity3.this, "执行修改动作", Toast.LENGTH_SHORT).show();
79         }
80 
81 
82         return super.onOptionsItemSelected(item);
83     }
84 }
OptionMenu

技术分享技术分享技术分享技术分享

三.上下文菜单  ContextMenu

   1.Activity是OnCreateContextMenuListener的实现类

   2.创建:组件.setOnCreateContextMenuListener(this):在代码里添加MenuItem,方式同 选择菜单。

   3.响应: 重写Activity的boolean   onContextItemSelected(MenuItem item)

   4.与LongClick监听器的关系

     1>LongClick监听器的优先级高

     2>boolean onLongClick(View v)的返回值决定后续的监听器能否再继续处理

技术分享
 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.example.wang.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 
19     <Button
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="上下文菜单按钮"
23         android:id="@+id/bt_2"/>
24 
25 </LinearLayout>
ContextMenu
技术分享
  1 package com.example.wang.testapp2;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.view.ContextMenu;
  7 import android.view.Menu;
  8 import android.view.MenuInflater;
  9 import android.view.MenuItem;
 10 import android.view.SubMenu;
 11 import android.view.View;
 12 import android.widget.Button;
 13 import android.widget.Toast;
 14 
 15 public class TestActivity3 extends Activity {
 16 
 17     Button bt_1;
 18     Button bt_2;
 19 
 20     @Override
 21     protected void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         setContentView(R.layout.activity_test3);
 24 
 25         bt_1=(Button)findViewById(R.id.bt_1);
 26         bt_2=(Button)findViewById(R.id.bt_2);
 27 
 28         //给组件添加上下文菜单  Activity是OnCreateContextMenuListener的实现类
 29         bt_2.setOnCreateContextMenuListener(
 30                 this
 31         );
 32 
 33         //添加长按监听器
 34        bt_2.setOnLongClickListener(new View.OnLongClickListener() {
 35            @Override
 36             public boolean onLongClick(View v) {
 37 
 38                Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
 39 
 40                return false;//true后面不在处理
 41             }
 42         });
 43         //点击
 44           bt_1.setOnClickListener(new View.OnClickListener() {
 45         @Override
 46         public void onClick(View v) {
 47 
 48             Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
 49         }
 50     });
 51 
 52 
 53     //长按
 54     bt_1.setOnLongClickListener(new View.OnLongClickListener() {
 55         @Override
 56         public boolean onLongClick(View v) {
 57 
 58 
 59             Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
 60 
 61             //事件是否已经被消费
 62             return true;
 63         }
 64     });
 65 }
 66 
 67     //重写创建上下文菜单的方法
 68     @Override
 69     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
 70         super.onCreateContextMenu(menu, v, menuInfo);
 71 
 72         menu.add(1, 0, 1, "字体大小");
 73         menu.add(1,1,2,"背景颜色");
 74 
 75 
 76         //加载文件
 77         MenuInflater m=getMenuInflater();
 78         m.inflate(R.menu.test3_menu,menu);
 79     }
 80 
 81     @Override
 82     public boolean onContextItemSelected(MenuItem item) {
 83 
 84         Toast.makeText(TestActivity3.this, "上下文菜单的title="+item.getTitle(), Toast.LENGTH_SHORT).show();
 85 
 86         return super.onContextItemSelected(item);
 87     }
 88 
 89     //给Activity 添加选项菜单
 90     //重写
 91     @Override
 92     public boolean onCreateOptionsMenu(Menu menu) {
 93 
 94 
 95         //加载菜单文件方式
 96         //1-获得菜单加载器
 97         MenuInflater m=getMenuInflater();
 98 
 99         //2-加载菜单文件  1)菜单文件 2)当前菜单
100         m.inflate(R.menu.test3_menu,menu);
101 
102 
103         return super.onCreateOptionsMenu(menu);
104     }
105 
106 }
ContextMenu

 技术分享

 

View(视图)———菜单Menu

标签:

原文地址:http://www.cnblogs.com/arxk/p/5476075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!