Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android Context Menu สร้างเมนูแบบ Context Menu แบบง่าย ๆ


Android Context Menu สร้างเมนูแบบ Context Menu แบบง่าย ๆ

Android Context Menu สำหรับ Context Menu จะเป็นเมนูที่อยู่ในรูปแบบของ Dialog Popup ที่สามารถสร้างเมนูได้หลาย ๆ ตัว เมนูนี้จะปรากฏก็ตือเมื่อมีการใช้ Long Click หรือ onLongPress โดย Context Menu สามาระประยุกต์ใช้กงานกับ Widgets หลาย ๆ ตัว ที่รองรับการทำงานผ่าน Event ของ onLongPress() เช่น Widgets Button , Widgets ListView , Widgets GridView และอื่น ๆ อีกหลายตัว

Android Context Menu

Android Context Menu Screenshot


Context Menu จะอยู่ภายใต้ Library ของ View ที่สามารถสร้างและใช้งานได้ง่าย ๆ และการอ่านค่าที่ผ่านจากการคลิกที่ Menu ก็สามารถใช้งานได้ง่ายเช่นเดียวกัน

1.menu.setHeaderTitle("Context Menu"); 
2.menu.add(0, 1, 1, "Menu 1"); 
3.menu.add(0, 2, 2, "Menu 2");
4.menu.add(0, 3, 3, "Menu 3");
5.menu.add(0, 4, 4, "Menu 4");

รูปแบบการสร้างเมนู

Example 1 การสร้าง Context Menu แบบง่าย ๆ

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

Android Context Menu

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <Button
07.        android:id="@+id/button1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentTop="true"
11.        android:layout_centerHorizontal="true"
12.        android:layout_marginTop="53dp"
13.        android:text="Menu 1" />
14. 
15.</RelativeLayout>


MainActivity.java
01.package com.myapp;
02. 
03.import android.os.Bundle;
04.import android.view.ContextMenu;
05.import android.view.ContextMenu.ContextMenuInfo;
06.import android.view.Menu;
07.import android.view.MenuItem;
08.import android.view.View;
09.import android.widget.Button;
10.import android.widget.Toast;
11.import android.app.Activity;
12. 
13. 
14.public class MainActivity extends Activity {
15.     
16.    String[] Cmd = {"Command1","Command2","Command3","Command4"};
17.     
18.    @Override
19.    public void onCreate(Bundle savedInstanceState) {
20.        super.onCreate(savedInstanceState);
21.        setContentView(R.layout.activity_main);
22.         
23.        // button1
24.        final Button btn1 = (Button)findViewById(R.id.button1);
25.         
26.        registerForContextMenu(btn1);
27.    }  
28.     
29.    @Override
30.    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
31. 
32.        menu.setHeaderIcon(android.R.drawable.btn_star_big_on);
33.        menu.setHeaderTitle("Menu 1");
34.        String[] menuItems = Cmd;
35.        for (int i = 0; i<menuItems.length; i++) {
36.            menu.add(Menu.NONE, i, i, menuItems[i]);
37.        }
38.    }
39.     
40.    @Override
41.    public boolean onContextItemSelected(MenuItem item) {
42.        int menuItemIndex = item.getItemId();
43.        String[] menuItems = Cmd;
44.        String CmdName = menuItems[menuItemIndex];
45.         
46.        // Check Event Command
47.        if ("Command1".equals(CmdName)) {
48.             Toast.makeText(MainActivity.this,"Your Selected Command1",Toast.LENGTH_LONG).show();
49.             /**
50.              * Call the mthod
51.              * Eg: Command1();
52.              */
53.        } else if ("Command2".equals(CmdName)) {
54.            Toast.makeText(MainActivity.this,"Your Selected Command2",Toast.LENGTH_LONG).show();
55.             /**
56.              * Call the mthod
57.              * Eg: Command2();
58.              */
59.        } else if ("Command3".equals(CmdName)) {
60.             Toast.makeText(MainActivity.this,"Your Selected Command3",Toast.LENGTH_LONG).show();
61.             /**
62.              * Call the mthod
63.              * Eg: Command3();
64.              */
65.        } else if ("Command4".equals(CmdName)) {
66.             Toast.makeText(MainActivity.this,"Your Selected Command4",Toast.LENGTH_LONG).show();
67.             /**
68.              * Call the mthod
69.              * Eg: Command4();
70.              */
71.        }
72.         
73.        return true;
74.    }
75. 
76.     
77.    @Override
78.    public boolean onCreateOptionsMenu(Menu menu) {
79.        getMenuInflater().inflate(R.menu.activity_main, menu);
80.        return true;
81.    }
82. 
83.}


คำอธิบาย

1.String[] Cmd = {"Command1","Command2","Command3","Command4"};

ประกาศ Menu แบบ Array เพื่อง่ายต่อการนำไปใช้

1.if ("Command1".equals(CmdName)) {
2.     Toast.makeText(MainActivity.this,"Your Selected Command1",Toast.LENGTH_LONG).show();
3.     /**
4.      * Call the mthod
5.      * Eg: Command1();
6.      */
7.}

เป็นการอ่านว่าได้เลือก Menu จากรายการที่เลือก



Screenshot

Android Context Menu

คลิกที่ Button ที่ Menu 1

Android Context Menu

แสดง Command ที่มาจาก Context Menu

Android Context Menu

เมื่อเลือก Menu





Example 2 การสร้าง Context Menu แบบหลาย Menu

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <Button
07.        android:id="@+id/button1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentTop="true"
11.        android:layout_centerHorizontal="true"
12.        android:layout_marginTop="53dp"
13.        android:text="Menu 1" />
14. 
15.    <Button
16.        android:id="@+id/button2"
17.        android:layout_width="wrap_content"
18.        android:layout_height="wrap_content"
19.        android:layout_alignLeft="@+id/button1"
20.        android:layout_below="@+id/button1"
21.        android:layout_marginTop="52dp"
22.        android:text="Menu 2" />
23. 
24.</RelativeLayout>


MainActivity.java
001.package com.myapp;
002. 
003.import android.os.Bundle;
004.import android.view.ContextMenu;
005.import android.view.ContextMenu.ContextMenuInfo;
006.import android.view.Menu;
007.import android.view.MenuItem;
008.import android.view.View;
009.import android.widget.Button;
010.import android.widget.Toast;
011.import android.app.Activity;
012. 
013. 
014.public class MainActivity extends Activity {
015.     
016.    String[] Cmd1 = {"Command1","Command2","Command3","Command4"};
017.    String[] Cmd2 = {"Command5","Command6","Command7","Command8"};
018.     
019.    Button btn1;
020.    Button btn2;
021.     
022.    String btnClick;
023.     
024.    @Override
025.    public void onCreate(Bundle savedInstanceState) {
026.        super.onCreate(savedInstanceState);
027.        setContentView(R.layout.activity_main);
028.         
029.        // button1
030.        btn1 = (Button)findViewById(R.id.button1);
031.         
032.        // button2
033.        btn2 = (Button)findViewById(R.id.button2);
034.         
035.        registerForContextMenu(btn1);
036.        registerForContextMenu(btn2);
037.    }  
038.     
039.    @Override
040.    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
041. 
042.        menu.setHeaderIcon(android.R.drawable.btn_star_big_on);
043.         
044.        if (v == btn1) {
045.            btnClick = "Cmd1";
046.            String[] menuItems = Cmd1;
047.            menu.setHeaderTitle("Menu 1");
048.            for (int i = 0; i<menuItems.length; i++) {
049.                menu.add(Menu.NONE, i, i, menuItems[i]);
050.            }
051.        } else if (v == btn2) {
052.            btnClick = "Cmd2";
053.            String[] menuItems = Cmd2;
054.            menu.setHeaderTitle("Menu 2");
055.            for (int i = 0; i<menuItems.length; i++) {
056.                menu.add(Menu.NONE, i, i, menuItems[i]);
057.            }
058.        }
059.         
060. 
061.    }
062.     
063.    @Override
064.    public boolean onContextItemSelected(MenuItem item) {
065.        int menuItemIndex = item.getItemId();
066.         
067.        // for Cmd1
068.        if(btnClick == "Cmd1")
069.        {
070.             
071.            String[] menuItems = Cmd1;
072.            String CmdName = menuItems[menuItemIndex];
073.             
074.            // Check Event Command
075.            if ("Command1".equals(CmdName)) {
076.                 Toast.makeText(MainActivity.this,"Your Selected Command1",Toast.LENGTH_LONG).show();
077.                 /**
078.                  * Call the mthod
079.                  * Eg: Command1();
080.                  */
081.            } else if ("Command2".equals(CmdName)) {
082.             Toast.makeText(MainActivity.this,"Your Selected Command2",Toast.LENGTH_LONG).show();
083.             /**
084.              * Call the mthod
085.              * Eg: Command2();
086.              */
087.            } else if ("Command3".equals(CmdName)) {
088.                 Toast.makeText(MainActivity.this,"Your Selected Command3",Toast.LENGTH_LONG).show();
089.                 /**
090.                  * Call the mthod
091.                  * Eg: Command3();
092.                  */
093.            } else if ("Command4".equals(CmdName)) {
094.                 Toast.makeText(MainActivity.this,"Your Selected Command4",Toast.LENGTH_LONG).show();
095.                 /**
096.                  * Call the mthod
097.                  * Eg: Command4();
098.                  */
099.            }
100. 
101.        }
102.         
103.        // for Cmd2
104.        if(btnClick == "Cmd2")
105.        {
106.             
107.            String[] menuItems = Cmd2;
108.            String CmdName = menuItems[menuItemIndex];
109.             
110.            // Check Event Command
111.            if ("Command5".equals(CmdName)) {
112.                 Toast.makeText(MainActivity.this,"Your Selected Command5",Toast.LENGTH_LONG).show();
113.                 /**
114.                  * Call the mthod
115.                  * Eg: Command5();
116.                  */
117.            } else if ("Command6".equals(CmdName)) {
118.                Toast.makeText(MainActivity.this,"Your Selected Command6",Toast.LENGTH_LONG).show();
119.                 /**
120.                  * Call the mthod
121.                  * Eg: Command6();
122.                  */
123.            } else if ("Command7".equals(CmdName)) {
124.                 Toast.makeText(MainActivity.this,"Your Selected Command7",Toast.LENGTH_LONG).show();
125.                 /**
126.                  * Call the mthod
127.                  * Eg: Command7();
128.                  */
129.            } else if ("Command8".equals(CmdName)) {
130.                 Toast.makeText(MainActivity.this,"Your Selected Command8",Toast.LENGTH_LONG).show();
131.                 /**
132.                  * Call the mthod
133.                  * Eg: Command8();
134.                  */
135.            }
136. 
137.        }
138.         
139.        return true;
140.    }
141. 
142.     
143.    @Override
144.    public boolean onCreateOptionsMenu(Menu menu) {
145.        getMenuInflater().inflate(R.menu.activity_main, menu);
146.        return true;
147.    }
148. 
149.}




จาก Code ของ Java

1.String[] Cmd1 = {"Command1","Command2","Command3","Command4"};
2.String[] Cmd2 = {"Command5","Command6","Command7","Command8"};

สร้างเมนูขึ้นมา 2 รายการจากค่า Array ของ Cmd1 และ Cmd2

Screenshot

Android Context Menu

มีอยู่ 2 Button

Android Context Menu

Menu จาก Menu 1

Android Context Menu

Menu จาก Menu 2

Android Context Menu

เมื่อคลิกเลิอกที่ Menu

ตัวอย่างการใช้ Context Menu



Android Delete Rows Data in SQLite Database (Android SQLite)

Android ImageView และ GridView แสดงรูปภาพบน GridView (GridView Column)

Android กับ ListView แสดงข้อมูลจาก Database ของ SQLite


   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-11 15:39:49 / 2017-03-26 21:42:36
  Download : No files
 Sponsored Links / Related

 
Android SlidingDrawer Menu
Rating :

 
Android Options Menus
Rating :

 
Android and Custom Tabs Menu (TabHost , TabWidget)
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่