【问题标题】:How to display a button on long press of a list item如何在长按列表项时显示按钮
【发布时间】:2012-08-21 14:51:00
【问题描述】:

我需要在长按列表项时显示删除按钮..

我有长按的代码..但不知道如何在长按中显示按钮...

【问题讨论】:

    标签: android listview button long-press


    【解决方案1】:

    终于找到答案了……

    .xml 文件

    <ImageButton
            android:id="@+id/imgdelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/delete" 
            android:visibility="invisible"/>
    

    .java 文件

          lv.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    
                arg1.findViewById(R.id.imgdelete).setVisibility(View.VISIBLE);
                return false;
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      首先,您必须使用代码使删除按钮不可见,或者在 xml 文件中设置它的属性。当用户单击longpress 时,您必须使该删除按钮可见。删除操作完成后,再次使该按钮不可见。

      【讨论】:

        【解决方案3】:

        您可以使用警报对话框。这是一个例子

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        
                    public boolean onItemLongClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        final CharSequence[] items = { "Delete Item" };
        
        
                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                [CLASS_NAME].this);
                        builder.setTitle("Delete Item");
                        builder.setItems(items, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                Intent i;
                                switch (item) {
                                case 0:
                                    AlertDialog.Builder builder = new AlertDialog.Builder(
                                            SelectProfile.this);
                                    builder.setMessage(
                                            "Are you sure you want to delete?")
                                            .setCancelable(false)
                                            // Prevents user to use "back button"
                                            .setPositiveButton(
                                                    "Delete",
                                                    new DialogInterface.OnClickListener() {
                                                        public void onClick(
                                                                DialogInterface dialog,
                                                                int id) {
                                                            //Todo code here
                                                        }
                                                    })
                                            .setNegativeButton(
                                                    "Cancel",
                                                    new DialogInterface.OnClickListener() {
                                                        public void onClick(
                                                                DialogInterface dialog,
                                                                int id) {
                                                            dialog.cancel();
                                                        }
                                                    });
                                    builder.show();
                                    break;
                                }
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                        return false;
                    }
                });
        

        【讨论】:

          猜你喜欢
          • 2021-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-26
          • 1970-01-01
          • 2020-06-26
          • 1970-01-01
          • 2011-11-29
          相关资源
          最近更新 更多