Use the CreateOptionsMenu and onCreateContextMenu menus of Android to simulate the pop-up menu options of WeChat chat interface ., Oncreateoptionsmenu

Source: Internet
Author: User

Use the CreateOptionsMenu and onCreateContextMenu menus of Android to simulate the pop-up menu options of the chat interface ., Oncreateoptionsmenu

1 package com. lixu. menu2; 2 3 import java. util. arrayList; 4 5 import android. app. activity; 6 import android. OS. bundle; 7 import android. view. contextMenu; 8 import android. view. menu; 9 import android. view. menuItem; 10 import android. view. view; 11 import android. view. contextMenu. contextMenuInfo; 12 import android. widget. adapterView. adapterContextMenuInfo; 13 import android. widget. arrayAdapter; 14 imp Ort android. widget. listView; 15 import android. widget. toast; 16 17 public class MainActivity extends Activity {18 private ArrayList <String> data; 19 private ArrayAdapter <String> mAdapter; 20 21 @ Override 22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity_main); 25 26 data = new ArrayList <String> (); 27 for (int I = 0; I <20; I ++) 28 data. add ("" + I); 29 30 ListView lv = (ListView) findViewById (R. id. listview); 31 mAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, data); 32 33 lv. setAdapter (mAdapter); 34 lv. setOnCreateContextMenuListener (this); 35} 36 37 @ Override 38 public boolean onCreateOptionsMenu (Menu menu) {39 getMenuInflater (). inflate (R. menu. main, menu); 40 return true; 41} 42 43 @ Override 44 public boolean onContextItemSelected (MenuItem item) {45 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo (); 46 int pos = info. position; 47 switch (item. getItemId () {48 case 1001: 49 Toast. makeText (this, "dear, this message has not been read! ", 0 ). show (); 50 break; 51 case 1002: 52 53 String s = data. get (pos); 54 data. remove (pos); 55 data. add (0, s); 56 57 mAdapter. notifyDataSetChanged (); 58 59 Toast. makeText (this, "Top chat! ", 0 ). show (); 60 break; 61 case 1003: 62 63 data. remove (pos); 64 mAdapter. notifyDataSetChanged (); 65 66 Toast. makeText (this, "deleted! ", 0 ). show (); 67 break; 68 69 default: 70 break; 71 72} 73 return super. onContextItemSelected (item); 74} 75 76 @ Override 77 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {78 super. onCreateContextMenu (menu, v, menuInfo); 79 menu. add (0, 1001,101, "marked as unread"); 80 menu. add (0, 1002,102, "Top chat"); 81 menu. add (0, 1003,103, "delete this chat"); 82} 83 84 @ Override 85 public bo Olean onOptionsItemSelected (MenuItem item) {86 int id = item. getItemId (); 87 switch (id) {88 case R. id. action_settings: 89 Toast. makeText (this, "dear, what do you want to set? ", 0). show (); 90 break; 91 case R. id. action_about: 92 Toast. makeText (this," dear, what do you want to see? ", 0). show (); 93 break; 94 case R. id. action_open: 95 Toast. makeText (this," dear, can I open it for you? ", 0). show (); 96 break; 97 case R. id. action_file: 98 Toast. makeText (this," dear, you know. ", 0). show (); 99 break; 100 101 default: 102 break; 103} 104 return super. onOptionsItemSelected (item); 105} 106}

Run:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.