zoukankan      html  css  js  c++  java
  • 运用安卓菜单的各种用法,模拟一些常用的弹出菜单选项。

      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.Context;
      7 import android.content.DialogInterface;
      8 import android.content.DialogInterface.OnClickListener;
      9 import android.content.DialogInterface.OnMultiChoiceClickListener;
     10 import android.os.Bundle;
     11 import android.view.ContextMenu;
     12 import android.view.Menu;
     13 import android.view.MenuItem;
     14 import android.view.View;
     15 import android.view.ViewGroup;
     16 import android.view.ContextMenu.ContextMenuInfo;
     17 import android.view.LayoutInflater;
     18 import android.widget.AdapterView;
     19 import android.widget.AdapterView.AdapterContextMenuInfo;
     20 import android.widget.AdapterView.OnItemClickListener;
     21 import android.widget.AdapterView.OnItemSelectedListener;
     22 import android.widget.ArrayAdapter;
     23 import android.widget.ImageView;
     24 import android.widget.ListView;
     25 import android.widget.TextView;
     26 import android.widget.Toast;
     27 
     28 public class MainActivity extends Activity {
     29     private ArrayList<String> data;
     30     private ArrayAdapter<String> mAdapter;
     31     private ArrayAdapter<String> mAdapter1;
     32     private Activity context;
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37         setContentView(R.layout.activity_main);
     38 
     39         context = this;
     40         data = new ArrayList<String>();
     41         for (int i = 0; i < 20; i++)
     42             data.add("大神" + i);
     43 
     44         ListView lv = (ListView) findViewById(R.id.listview);
     45         mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
     46 
     47         lv.setAdapter(mAdapter);
     48         lv.setOnCreateContextMenuListener(this);
     49 
     50     }
     51 
     52     // 创建右上角点击下拉菜单
     53     @Override
     54     public boolean onCreateOptionsMenu(Menu menu) {
     55         getMenuInflater().inflate(R.menu.main, menu);
     56         return true;
     57     }
     58 
     59     // 创建界面长按点击弹出菜单的点击事件
     60     @Override
     61     public boolean onContextItemSelected(MenuItem item) {
     62         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
     63         final int pos = info.position;
     64         switch (item.getItemId()) {
     65         case 1001:
     66             Toast.makeText(this, "亲,这条消息未读哦!", 0).show();
     67             break;
     68         case 1002:
     69             // 对话提示选择框
     70             AlertDialog dialog = new AlertDialog.Builder(this).create();
     71             dialog.setTitle("对话框");
     72             dialog.setIcon(R.drawable.ic_launcher);
     73             dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
     74 
     75                 @Override
     76                 public void onClick(DialogInterface dialog, int which) {
     77                     String s = data.get(pos);
     78 
     79                     data.remove(pos);
     80                     data.add(0, s);
     81 
     82                     mAdapter.notifyDataSetChanged();
     83 
     84                     Toast.makeText(getApplicationContext(), "聊天已置顶!", 0).show();
     85                 }
     86             });
     87 
     88             dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
     89 
     90                 @Override
     91                 public void onClick(DialogInterface dialog, int which) {
     92                     String s = data.get(pos);
     93 
     94                     Toast.makeText(getApplicationContext(), "已取消!", 0).show();
     95                 }
     96             });
     97             dialog.show();
     98 
     99             break;
    100         case 1003:
    101             // 对话提示选择框
    102             AlertDialog dialog1 = new AlertDialog.Builder(this).create();
    103             dialog1.setTitle("对话框");
    104             dialog1.setIcon(R.drawable.ic_launcher);
    105             dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
    106 
    107                 @Override
    108                 public void onClick(DialogInterface dialog, int which) {
    109                     String s = data.get(pos);
    110 
    111                     data.remove(pos);
    112                     mAdapter.notifyDataSetChanged();
    113                     Toast.makeText(getApplicationContext(), "已删除!", 0).show();
    114 
    115                 }
    116             });
    117 
    118             dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
    119 
    120                 @Override
    121                 public void onClick(DialogInterface dialog, int which) {
    122 
    123                     Toast.makeText(getApplicationContext(), "已取消!", 0).show();
    124                 }
    125             });
    126             dialog1.show();
    127             break;
    128 
    129         case 1004:
    130             // 单项选择对话框
    131             final String[] items = { "转发到QQ空间", "转发到微信朋友圈", "转发到新浪微博" };
    132 
    133             AlertDialog.Builder dialog2 = new AlertDialog.Builder(this);
    134             dialog2.setTitle("转发");
    135             dialog2.setIcon(R.drawable.ic_launcher);
    136             // 0 代表初始选择 "转发到QQ空间"
    137             dialog2.setSingleChoiceItems(items, 0, new OnClickListener() {
    138 
    139                 @Override
    140                 public void onClick(DialogInterface dialog, int which) {
    141                     // 处理自己的逻辑运算
    142                     Toast.makeText(getApplicationContext(), "已" + items[which] + "!", Toast.LENGTH_SHORT).show();
    143                     dialog.dismiss();
    144 
    145                 }
    146             });
    147 
    148             dialog2.create().show();
    149 
    150             break;
    151         case 1005:
    152             // 多项选择对话框
    153             final String[] items1 = { "同时转发到QQ空间", "同时转发到微信朋友圈", "同时转发到新浪微博" };
    154 
    155             AlertDialog.Builder dialog3 = new AlertDialog.Builder(this);
    156 
    157             dialog3.setIcon(R.drawable.ic_launcher);
    158             dialog3.setTitle("多项转发");
    159             final boolean[] checkedItems = { false, false, false };
    160             // 点击选择事件来改变boolean数组里面的值
    161             dialog3.setMultiChoiceItems(items1, checkedItems, new OnMultiChoiceClickListener() {
    162 
    163                 @Override
    164                 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    165 
    166                 }
    167             });
    168             AlertDialog mdialog = dialog3.create();
    169             mdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {
    170 
    171                 @Override
    172                 public void onClick(DialogInterface dialog, int which) {
    173                     String s = "";
    174                     // 将boolean数组进行遍历
    175                     for (int i = 0; i < checkedItems.length; i++) {
    176                         // 当boolean数组里面值为true时 证明被选中 后添加入s
    177                         if (checkedItems[i])
    178                             s += items1[i] + "
    ";
    179                     }
    180 
    181                     Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
    182 
    183                 }
    184             });
    185             mdialog.show();
    186             break;
    187 
    188         case 1006:
    189             // 设置自定义的弹出框布局 这里用到listview 这里找到布局不能直接findViewById
    190             // 要从getLayoutInflater().inflate(R.layout.listview, null);这样得到
    191             View view = getLayoutInflater().inflate(R.layout.listview, null);
    192 
    193             ListView lv1 = (ListView) view.findViewById(R.id.listview2);
    194 
    195             // 添加数据
    196             String[] s = new String[50];
    197             for (int i = 0; i < s.length; i++)
    198                 s[i] = "数据" + i;
    199             mAdapter1 = new MyAdapter(this, -1, s);
    200 
    201             lv1.setAdapter(mAdapter1);
    202 
    203             AlertDialog.Builder dialog4 = new AlertDialog.Builder(this);
    204 
    205             dialog4.setTitle("列表");
    206             // 设置布局
    207             dialog4.setView(view);
    208 
    209             dialog4.create().show();
    210             lv1.setOnItemClickListener(new OnItemClickListener() {
    211 
    212                 @Override
    213                 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    214 
    215                     Toast.makeText(getApplicationContext(), "您选择了" + pos, Toast.LENGTH_SHORT).show();
    216 
    217                 }
    218             });
    219 
    220             break;
    221 
    222         case 1007:
    223             context.openOptionsMenu();
    224             break;
    225 
    226         default:
    227             break;
    228 
    229         }
    230         return super.onContextItemSelected(item);
    231     }
    232 
    233     private class MyAdapter extends ArrayAdapter<String> {
    234         private LayoutInflater flater;
    235         private String[] s;
    236 
    237         public MyAdapter(Context context, int resource, String[] s) {
    238             super(context, resource);
    239             this.s = s;
    240             flater = LayoutInflater.from(context);
    241 
    242         }
    243 
    244         @Override
    245         public View getView(int position, View convertView, ViewGroup parent) {
    246             if (convertView == null)
    247                 convertView = flater.inflate(R.layout.list, null);
    248 
    249             ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
    250             iv.setImageResource(R.drawable.dfdf);
    251             TextView tv = (TextView) convertView.findViewById(R.id.tv);
    252             tv.setText(s[position]);
    253 
    254             return convertView;
    255         }
    256 
    257         @Override
    258         public int getCount() {
    259             return s.length;
    260         }
    261 
    262     }
    263 
    264     // 创建界面长按点击弹出菜单
    265     @Override
    266     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    267         super.onCreateContextMenu(menu, v, menuInfo);
    268         menu.add(0, 1001, 101, "标记为未读");
    269         menu.add(0, 1002, 102, "置顶聊天");
    270         menu.add(0, 1003, 103, "删除该聊天");
    271         menu.add(0, 1004, 104, "转发到:");
    272         menu.add(0, 1005, 105, "同时转发到:");
    273         menu.add(0, 1006, 106, "listview:");
    274         menu.add(0, 1007, 107, "打开设置");
    275 
    276     }
    277 
    278     // 创建右上角点击下拉菜单的点击事件
    279     @Override
    280     public boolean onOptionsItemSelected(MenuItem item) {
    281         int id = item.getItemId();
    282         switch (id) {
    283         case R.id.action_settings:
    284             // 多选对话框
    285             String[] items = { "音量", "亮度", "背景" };
    286             AlertDialog dialog = new AlertDialog.Builder(this).setItems(items, new OnClickListener() {
    287 
    288                 @Override
    289                 public void onClick(DialogInterface dialog, int which) {
    290 
    291                     switch (which) {
    292                     case 0:
    293                         Toast.makeText(getApplicationContext(), "音量设置完成!", 0).show();
    294                         break;
    295                     case 1:
    296                         Toast.makeText(getApplicationContext(), "亮度设置完成!", 0).show();
    297                         break;
    298                     case 2:
    299                         Toast.makeText(getApplicationContext(), "背景设置完成!", 0).show();
    300                         break;
    301 
    302                     default:
    303                         break;
    304                     }
    305 
    306                 }
    307             }).create();
    308             dialog.setTitle("设置");
    309             dialog.setIcon(R.drawable.ic_launcher);
    310 
    311             dialog.show();
    312 
    313             break;
    314         case R.id.action_about:
    315             Toast.makeText(getApplicationContext(), "作者:木子小新", 1).show();
    316             break;
    317         case R.id.action_open:
    318             String[] items1 = { "新建", "打开" };
    319             AlertDialog dialog1 = new AlertDialog.Builder(this).setItems(items1, new OnClickListener() {
    320 
    321                 @Override
    322                 public void onClick(DialogInterface dialog, int which) {
    323 
    324                     switch (which) {
    325                     case 0:
    326                         Toast.makeText(getApplicationContext(), "新建完成!", 0).show();
    327                         break;
    328                     case 1:
    329                         Toast.makeText(getApplicationContext(), "打开完成!", 0).show();
    330                         break;
    331 
    332                     default:
    333                         break;
    334                     }
    335 
    336                 }
    337             }).create();
    338             dialog1.setTitle("文件");
    339             dialog1.setIcon(R.drawable.ic_launcher);
    340 
    341             dialog1.show();
    342 
    343             break;
    344         case R.id.action_file:
    345             Toast.makeText(this, "亲,你懂的。", 0).show();
    346             break;
    347 
    348         default:
    349             break;
    350         }
    351         return super.onOptionsItemSelected(item);
    352     }
    353 }

    xml——menu文件:

     1 <menu xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="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     <item
    11         android:id="@+id/action_about"
    12         android:orderInCategory="104"
    13         android:showAsAction="never"
    14         android:title="关于"/>
    15     <item
    16         android:id="@+id/action_file"
    17         android:orderInCategory="102"
    18         android:showAsAction="never"
    19         android:title="文件"/>
    20     <item
    21         android:id="@+id/action_open"
    22         android:orderInCategory="101"
    23         android:showAsAction="never"
    24         android:title="打开"
    25         />
    26 
    27 </menu>

    运行效果图:

  • 相关阅读:
    工作所得所思之一
    angular路由
    html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    您无权输入许可证密钥,请请使用系统管理员账户重试
    nginx的配置说明
    flexbox弹性盒子模型
    移动web开发规范
    为什么要使用响应式开发
    移动web开发
    如何给图片做缓存
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/5033643.html
Copyright © 2011-2022 走看看