你的位置:首页 > 操作系统

[操作系统]运用安卓菜单的各种用法,模拟一些常用的弹出菜单选项。


 1 package com.lixu.menu2; 2  3 import java.util.ArrayList; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.content.DialogInterface; 7 import android.content.DialogInterface.OnClickListener; 8 import android.content.DialogInterface.OnMultiChoiceClickListener; 9 import android.os.Bundle; 10 import android.view.ContextMenu; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.View; 14 import android.view.ContextMenu.ContextMenuInfo; 15 import android.widget.AdapterView.AdapterContextMenuInfo; 16 import android.widget.ArrayAdapter; 17 import android.widget.ListView; 18 import android.widget.Toast; 19  20 public class MainActivity extends Activity { 21   private ArrayList<String> data; 22   private ArrayAdapter<String> mAdapter; 23  24   @Override 25   protected void onCreate(Bundle savedInstanceState) { 26     super.onCreate(savedInstanceState); 27     setContentView(R.layout.activity_main); 28  29     data = new ArrayList<String>(); 30     for (int i = 0; i < 20; i++) 31       data.add("大神" + i); 32  33     ListView lv = (ListView) findViewById(R.id.listview); 34     mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); 35  36     lv.setAdapter(mAdapter); 37     lv.setOnCreateContextMenuListener(this); 38   } 39  40   // 创建右上角点击下拉菜单 41   @Override 42   public boolean onCreateOptionsMenu(Menu menu) { 43     getMenuInflater().inflate(R.menu.main, menu); 44     return true; 45   } 46  47   // 创建界面长按点击弹出菜单的点击事件 48   @Override 49   public boolean onContextItemSelected(MenuItem item) { 50     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 51     final int pos = info.position; 52     switch (item.getItemId()) { 53     case 1001: 54       Toast.makeText(this, "亲,这条消息未读哦!", 0).show(); 55       break; 56     case 1002: 57       // 对话提示选择框 58       AlertDialog dialog = new AlertDialog.Builder(this).create(); 59       dialog.setTitle("对话框"); 60       dialog.setIcon(R.drawable.ic_launcher); 61       dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() { 62  63         @Override 64         public void onClick(DialogInterface dialog, int which) { 65           String s = data.get(pos); 66  67           data.remove(pos); 68           data.add(0, s); 69  70           mAdapter.notifyDataSetChanged(); 71  72           Toast.makeText(getApplicationContext(), "聊天已置顶!", 0).show(); 73         } 74       }); 75  76       dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { 77  78         @Override 79         public void onClick(DialogInterface dialog, int which) { 80           String s = data.get(pos); 81  82           Toast.makeText(getApplicationContext(), "已取消!", 0).show(); 83         } 84       }); 85       dialog.show(); 86  87       break; 88     case 1003: 89       // 对话提示选择框 90       AlertDialog dialog1 = new AlertDialog.Builder(this).create(); 91       dialog1.setTitle("对话框"); 92       dialog1.setIcon(R.drawable.ic_launcher); 93       dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() { 94  95         @Override 96         public void onClick(DialogInterface dialog, int which) { 97           String s = data.get(pos); 98  99           data.remove(pos);100           mAdapter.notifyDataSetChanged();101           Toast.makeText(getApplicationContext(), "已删除!", 0).show();102 103         }104       });105 106       dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {107 108         @Override109         public void onClick(DialogInterface dialog, int which) {110 111           Toast.makeText(getApplicationContext(), "已取消!", 0).show();112         }113       });114       dialog1.show();115       break;116 117     case 1004:118       // 单项选择对话框119       final String[] items = { "转发到QQ空间", "转发到微信朋友圈", "转发到新浪微博" };120 121       AlertDialog.Builder dialog2 = new AlertDialog.Builder(this);122       dialog2.setTitle("转发");123       dialog2.setIcon(R.drawable.ic_launcher);124       // 0 代表初始选择 "转发到QQ空间"125       dialog2.setSingleChoiceItems(items, 0, new OnClickListener() {126 127         @Override128         public void onClick(DialogInterface dialog, int which) {129           // 处理自己的逻辑运算130           Toast.makeText(getApplicationContext(), "已" + items[which] + "!", Toast.LENGTH_SHORT).show();131           dialog.dismiss();132 133         }134       });135 136       dialog2.create().show();137 138       break;139     case 1005:140       // 多项选择对话框141       final String[] items1 = { "同时转发到QQ空间", "同时转发到微信朋友圈", "同时转发到新浪微博" };142 143       AlertDialog.Builder dialog3 = new AlertDialog.Builder(this);144 145       dialog3.setIcon(R.drawable.ic_launcher);146       dialog3.setTitle("多项转发");147       final boolean[] checkedItems = { false, false, false };148       // 点击选择事件来改变boolean数组里面的值149       dialog3.setMultiChoiceItems(items1, checkedItems, new OnMultiChoiceClickListener() {150 151         @Override152         public void onClick(DialogInterface dialog, int which, boolean isChecked) {153 154         }155       });156       AlertDialog mdialog = dialog3.create();157       mdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {158 159         @Override160         public void onClick(DialogInterface dialog, int which) {161           String s = "";162           // 将boolean数组进行遍历163           for (int i = 0; i < checkedItems.length; i++) {164             // 当boolean数组里面值为true时 证明被选中 后添加入s165             if (checkedItems[i])166               s += items1[i] + "\n";167           }168           169           Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();170 171         }172       });173       mdialog.show();174       break;175 176     default:177       break;178 179     }180     return super.onContextItemSelected(item);181   }182 183   // 创建界面长按点击弹出菜单184   @Override185   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {186     super.onCreateContextMenu(menu, v, menuInfo);187     menu.add(0, 1001, 101, "标记为未读");188     menu.add(0, 1002, 102, "置顶聊天");189     menu.add(0, 1003, 103, "删除该聊天");190     menu.add(0, 1004, 104, "转发到:");191     menu.add(0, 1005, 105, "同时转发到:");192   }193 194   // 创建右上角点击下拉菜单的点击事件195   @Override196   public boolean onOptionsItemSelected(MenuItem item) {197     int id = item.getItemId();198     switch (id) {199     case R.id.action_settings:200       // 多选对话框201       String[] items = { "音量", "亮度", "背景" };202       AlertDialog dialog = new AlertDialog.Builder(this).setItems(items, new OnClickListener() {203 204         @Override205         public void onClick(DialogInterface dialog, int which) {206 207           switch (which) {208           case 0:209             Toast.makeText(getApplicationContext(), "音量设置完成!", 0).show();210             break;211           case 1:212             Toast.makeText(getApplicationContext(), "亮度设置完成!", 0).show();213             break;214           case 2:215             Toast.makeText(getApplicationContext(), "背景设置完成!", 0).show();216             break;217 218           default:219             break;220           }221 222         }223       }).create();224       dialog.setTitle("设置");225       dialog.setIcon(R.drawable.ic_launcher);226 227       dialog.show();228 229       break;230     case R.id.action_about:231       Toast.makeText(getApplicationContext(), "作者:木子小新", 1).show();232       break;233     case R.id.action_open:234       String[] items1 = { "新建", "打开" };235       AlertDialog dialog1 = new AlertDialog.Builder(this).setItems(items1, new OnClickListener() {236 237         @Override238         public void onClick(DialogInterface dialog, int which) {239 240           switch (which) {241           case 0:242             Toast.makeText(getApplicationContext(), "新建完成!", 0).show();243             break;244           case 1:245             Toast.makeText(getApplicationContext(), "打开完成!", 0).show();246             break;247 248           default:249             break;250           }251 252         }253       }).create();254       dialog1.setTitle("文件");255       dialog1.setIcon(R.drawable.ic_launcher);256 257       dialog1.show();258 259       break;260     case R.id.action_file:261       Toast.makeText(this, "亲,你懂的。", 0).show();262       break;263 264     default:265       break;266     }267     return super.onOptionsItemSelected(item);268   }269 }

 1 <menu ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   tools:context="com.lixu.menu2.MainActivity" > 4  5   <item 6     android:id="@+id/action_settings" 7     android:orderInCategory="103" 8     android:showAsAction="never" 9     android:title="设置"/>10   <item11     android:id="@+id/action_about"12     android:orderInCategory="104"13     android:showAsAction="never"14     android:title="关于"/>15   <item16     android:id="@+id/action_file"17     android:orderInCategory="102"18     android:showAsAction="never"19     android:title="文件"/>20   <item21     android:id="@+id/action_open"22     android:orderInCategory="101"23     android:showAsAction="never"24     android:title="打开"25     />26 27 </menu>

运行效果图: