Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 運用安卓CreateOptionsMenu和onCreateContextMenu菜單,模擬微信聊天界面長按彈出菜單選項。,oncreateoptionsmenu

運用安卓CreateOptionsMenu和onCreateContextMenu菜單,模擬微信聊天界面長按彈出菜單選項。,oncreateoptionsmenu

編輯:關於android開發

運用安卓CreateOptionsMenu和onCreateContextMenu菜單,模擬微信聊天界面長按彈出菜單選項。,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 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 }

運行效果圖:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved