开发者

How to prevent button onClick firing repeatly?

I have a Button which sets both onLongClickListener & onClickListener :

onLongClickListener :

                    @Override
                    public boolean onLongClick(View v) {
                            do something ...
                            return true;
                    }

onClickListener :

                    @Override
                    public void onClick(View v) {
                            do something else ...
                    }

When I long click the button, onLongClick fires repeatly (sometimes onClick fires too when I release the button, it's weird @@") What I want is to make the onLongClick be triggered only once for one long press. So I modified the code :

onLongClickListener :

                    @Override
                    public boolean onLongClick(View v) {
                            do so开发者_Python百科mething ...
                            myButton.setLongClickable(false);
                            return true;
                    }

onClickListener :

                    @Override
                    public void onClick(View v) {
                            myButton.setLongClickable(true);
                            do something else ...
                    }

Unfortunately, the onClick callback was locked too after onLongClick fires! I cant unlock the button anymore :| Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?


I've got the code you need, give me a minute to post it.

    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          //DO STUFF GRAH!
        }
    });
    btn.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            //OTHER STUFF
            return true;
        }
    });

This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜