Android button software failure

I have a simple one ImageButton

in my android program that when clicked adds "0" to TextView

. When this button is clicked, it should add "+" to TextView

. The program works fine, but I encountered the typical key bounce effect. When I long press the button, it adds "+", but when I release the button, it also adds "0". Android seems to register the second click when the long click ends. How can I fix this? That's what I'm doing:

ImageButton button0=(ImageButton)V.findViewById(R.id.imageButtonzero);
        button0.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                enterNumber.append("0");

            }
        });
        button0.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                enterNumber.append("+");
                return false;
            }
        });

      

Thank you for your help!

+3


source to share


1 answer


You need to return true

to OnLongClickListener

to inform other listeners that the event has been destroyed and do not need to proceed further down the line:

button0.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        enterNumber.append("+");
        return true;
    }
});

      



Information source: Android javadoc

+1


source







All Articles