【问题标题】:OnLongClickListener does not workOnLongClickListener 不起作用
【发布时间】:2014-02-03 20:42:13
【问题描述】:

这是我的代码...

TextView tvItem = (TextView) findViewById(R.id.tvItem);

ListView list = (ListView) findViewById(R.id.list);

tvItem.setLongClickable(true);

tvItem.setClickable(true);

list.setOnItemLongClickListener(new OnItemLongClickListener(){


    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
        alert.setMessage("What do you want to do?");
        alert.setPositiveButton("Rename", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alert.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            dialog.cancel();
            }
        });
        alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        AlertDialog ale=alert.create();
        ale.show();
        return false;
    }

正如我所说,OnLongClickListener 不起作用。

【问题讨论】:

    标签: java android eclipse onlongclicklistener


    【解决方案1】:

    在适配器中尝试如下:

    public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
     if (view == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.list_view_inspection, null);
            }
    
     view.setOnLongClickListener(new OnLongClickListener() {    
                        @Override
                        public boolean onLongClick(View v) {
                            Dialog(param 1, param2); //your alert box method or something
                            return true;
                        }
                    });
    }
    

    【讨论】:

      【解决方案2】:

      您应该在列表中设置setLongClickable(true);,例如:list.setLongClickable(true);

      【讨论】:

        【解决方案3】:

        您必须启用 longclickable

        list.setLongClickable(true);
        

        你在TextView Object 上调用了这个方法。

        【讨论】:

        • 我试过了,但是没用,还有什么建议吗?
        • 清理项目并再次运行......有时它会在不清理的情况下部署项目。
        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多