码迷,mamicode.com
首页 > 微信 > 详细

运用安卓CreateOptionsMenu和onCreateContextMenu菜单,模拟微信聊天界面长按弹出菜单选项。

时间:2015-12-09 19:30:27      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:

  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 import 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, "亲,这条消息未读哦!", 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, "聊天置顶!", 0).show();
 60             break;
 61         case 1003:
 62 
 63             data.remove(pos);
 64             mAdapter.notifyDataSetChanged();
 65 
 66             Toast.makeText(this, "已删除!", 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, "标记为未读");
 80         menu.add(0, 1002, 102, "置顶聊天");
 81         menu.add(0, 1003, 103, "删除该聊天");
 82     }
 83 
 84     @Override
 85     public boolean onOptionsItemSelected(MenuItem item) {
 86         int id = item.getItemId();
 87         switch (id) {
 88         case R.id.action_settings:
 89             Toast.makeText(this, "亲,想设置什么?", 0).show();
 90             break;
 91         case R.id.action_about:
 92             Toast.makeText(this, "亲,想看什么?", 0).show();
 93             break;
 94         case R.id.action_open:
 95             Toast.makeText(this, "亲,我帮你打开?", 0).show();
 96             break;
 97         case R.id.action_file:
 98             Toast.makeText(this, "亲,你懂的。", 0).show();
 99             break;
100 
101         default:
102             break;
103         }
104         return super.onOptionsItemSelected(item);
105     }
106 }

运行效果图:

技术分享

运用安卓CreateOptionsMenu和onCreateContextMenu菜单,模拟微信聊天界面长按弹出菜单选项。

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/5033643.html

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