OnLongClickListener不起作用

时间:2014-02-03 20:42:14

标签: java android eclipse onlongclicklistener

这是我的代码......

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不起作用。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您必须启用longclickable

list.setLongClickable(true);

您在TextView对象上调用了此方法。

答案 2 :(得分:0)

在适配器中尝试这样的事情:

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;
                    }
                });
}