关于PopupMenu的用法

Android应用中,时常需要弹出临时的子菜单,如很多应用中,长按就会弹出一些子选项菜单。在此记录ListView的items长按弹出子菜单的步骤。

1、在menu包中新建xml文件,决定菜单的内容与次序

    <item android:id="@+id/blakc_delete" android:title="恢复联系人"
        android:orderInCategory="100"
        app:showAsAction="never"/>
    <item android:id="@+id/black_info" android:title="查看详情"
        android:orderInCategory="99"
        app:showAsAction="never"/>

其中android:orderInCategory决定了子菜单item的顺序,数字小的出现在上面;如果item一样,就考虑它们在xml中的位置,写在上面的就出现在上面。

2、创建ListView的item长按触发事件

        ListView list = (ListView) findViewById(R.id.black_list);
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                return true;
            }
        });

position表示是ListView中的第几个item被长按了。

3、创建子菜单并获取这个menu界面

                PopupMenu popupMenu = new PopupMenu(BlackNumberInfo.this , view);
                getMenuInflater().inflate(R.menu.black_menu,popupMenu.getMenu());

4、创建子菜单的item点击事件,并用title来甄别点的是哪个item

                    public boolean onMenuItemClick(MenuItem item) {
                        if(item.getTitle().equals("恢复联系人")){
                            listManagerDelete(position);
                            Toast.makeText(BlackNumberInfo.this,"已恢复此联系人",Toast.LENGTH_SHORT).show();
                        }
                        else if(item.getTitle().equals("查看详情")){
                            Toast.makeText(BlackNumberInfo.this,"我啥也不会做。。。",Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                });

5、一定要显示子菜单,不然啥都没有!!!

                popupMenu.show();

以上

时间: 2024-08-04 20:03:43

关于PopupMenu的用法的相关文章

Android Support Design Library之TextInputLayout

相信普通的用户在使用手机的时候经常遇到一个输入框没输入的时候会有灰色的提示,可一旦输入些许字符后,用户很快忘记了这个输入框要输入什么,但是退回去却又要重新输入,对于用户的这个要求,谷歌看在眼里,在2015推出了TextInputLayout来满足这个需求.那么对于怎么使用这个控件,我将用模仿网易邮箱大师的登录界面来一一告诉大家怎么畅快的玩起来. 下面来看看,我们将要实现的界面. 1.TextInputLayout在布局里面的那些事 对于TextInputLayout并不能单独的使用,必须配合Ed

Android ListPopupWindow的使用

其实像ListPopupWindow.PopupMenu的用法大致和PopupWindow的一样!就不讲了,相信用过PopupWindow的看一下就能明白. 先上个效果图: ListPopupWindow可以用在下拉列表.查询结果显示.历史记录.提示输入等,当然PopupMenu也可以的. 直接贴代码吧: public class MainActivity extends ActionBarActivity { private EditText mEditText; private ListPo

Menu,SubMenu,popupMenu,contextMenu的xml用法

在android3.0中引入了menu菜单,通过menu的xml文件,我们能迅速新建生成一个菜单文件,接下来一一介绍几种常用的菜单. 1.menu的调用,重写两个方法即可实现 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu);//填充menu文件夹中的main.xml菜单l

delphi CxGrid用法总结(63问)

1. 去掉cxGrid中台头的Box 解决:在tableview1的optionsview的groupbybox=false; ***************************************************************************** 2.统计功能 解决:(1) tableview tableview1的optionsviewfooter=ture 然后在cxGRid1的customize..中的summary 的footer.add需要合计的列k

弹出式菜单(下拉菜单)实现——PopupMenu

PopupMenu代表弹出式菜单,它会在指定组件上弹出PopupMenu,默认情况下,PopupMenu会显示在该组件的下方或上方.PopupMenu可增加多个菜单项,并可为菜单项增加子菜单. 使用PopupMenu创建菜单的步骤非常简单,只要如下步骤即可. 调用new PopupMenu(Context context,View anchor)创建下拉菜单,anchor代表要激发该弹出菜单的组件. 调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中. 调用

delphi CXGRID用法 (取行、列值;定位选中某行等等)

CXGRID用法 (取行.列值:定位选中某行等等) Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DBTableView1.Controller.FocusedRow 当前行 cxGrid1DBTableView1.Controller.FocusedColumn 当前列 cxGrid1DBTableView1.Controller.Focused

cxGrid用法-最新

cxGrid用法步1.你先放一个cxGrid,设置好View,设置View.DataController连接的DataSource 2.激活DataSource连接的DataSet,双击cxGrid,点击Retrieve Fields,取得所有的Column 3.设置View的OptionsView.Footer=True,OptionsView.GroupFooters=True,这是为了把分组小计和总计面板显示出来 4.将“订单号”字段拖到cxGrid上方的分组面板(GroupbyBox),

js中获取时间new date()的用法

js中获取时间new date()的用法 获取时间:   var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getFullYear(); //获取完整的年份(4位,1970-????) 3 myDate.getMonth(); //获取当前月份(0-11,0代表1月) 4 myDate.getDate(); //获取当前日(1-31) 5 myDate.getDay();

20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)

扩展: select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html 20.5 Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 1. 创建if1.sh测试脚本: [[email protected] ~]# vi if1.sh a=5,如果a大于3,满足这个条件,显示ok 添加内容: #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi 2. 执行if1.sh脚本: [[e