implement performClick on android?

2

when I use the setOnTouchListener on a button, when I overwrite the onTouch method it tells me this

  

Custom View ImageView has setOnTouchListener enabled but does not replace performClick If a view that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and calls it when clicks are detected, the view may not handle accessibility actions correctly. The logic that handles click actions should ideally be placed in View # performClick since some accessibility services invoke performClick when a click action should occur

I have searched for everything and I can not find how to remove the warning.

This is my code:

boton_w.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){

                boton_w.setElevation(2);

            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){

                boton_w.setElevation(10);

            }
            return true;
        }
    });

That's how the warning marks me

    
asked by Jesus Rodriguez 06.12.2017 в 05:04
source

1 answer

3

Ok, try calling the performClick method like this:

boton_w.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
             switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                 boton_w.setElevation(2);
                break;
            case MotionEvent.ACTION_UP:
                view.performClick();
                boton_w.setElevation(10);
                break;
            default:
                break;
            }
            return true;
        }
    });




 boton_f.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                 switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                     boton_f.setElevation(2);
                    break;
                case MotionEvent.ACTION_UP:
                    view.performClick();
                    boton_f.setElevation(10);
                    break;
                default:
                    break;
                }
                return true;
            }
        });

Apparently with Android Studio 3.0 those warnings appear, you can do it in another way:

boton_f.setOnTouchListener(this);
boton_w.setOnTouchListener(this);


public class TuActividad extends Activity implements View.OnTouchListener {


 ...
      @Override
    public boolean onTouch(View v, MotionEvent event) {

        //int action = event.getAction();
        switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            if (v.getId() == boton_w.getId()){
                 boton_w.setElevation(2);
             } else {
                boton_f.setElevation(2);
             }
        break;

        case MotionEvent.ACTION_UP:
                    v.performClick();
                   if (v.getId() == boton_w.getId()){
                         boton_w.setElevation(10);
                     } else {
                        boton_f.setElevation(10);
                     }
        break;
        }
        return true;
    }
    
answered by 06.12.2017 / 05:34
source