ImageButton onlongpress causes onclick as well

2019-09-20 02:05发布

问题:

i have an application with an imagebutton that has both an onclick and an onlongclick listener. However, when the button is long pressed, both of these listeners are executing. Any suggestions?

d1.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            selectMode = true;
            dockNum = 1;
            sd1.open();
            d1.cancelLongPress();
            return false;
        }

    });

...d1.setOnClickListener(this);

...case R.id.d1:
        if(d1s.equals("empty")) {
            selectMode = true;
            dockNum = 1;
            sd1.open();
        } else {
            Intent d1i = pm.getLaunchIntentForPackage(d1s);
            startActivity(d1i);
        }
    break;

回答1:

I think your problem has to do with the fact that you're returning false in your onLongClick method. Try returning true instead (despite the fact that you're canceling the long click, returning true is just saying "I've handled this, no further action is required.").