简体   繁体   中英

Android Button onTouch(), if return true, has no click animation effect; if return false, has clik animation effect

I observe this weird behavior, please help me figure it out!

I simply set a onTouchListner to a button. For the onTouch() callback, if I set it return false, when I click on the button, I can see the click animation effect of the button (simply color change); However, if I set it return true, when I click on the button, the click animation effect just disappeared.

Below is the code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btn);
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setClickable(true);
                Log.d("myTrack", "onTouch");
                return true;  // I cannot see the animation effect when click
                return false; // I can see the animation effect when click
            }
        });
    }
}

I observe this weird behavior, please help me figure it out!

This is default behaviour of Android.

If you return true from an ACTION_DOWN event you are interested in the rest of the events in that gesture. A "gesture" in this case means all events until the final ACTION_UP or ACTION_CANCEL . Returning false from an ACTION_DOWN means you do not want the event and other views will have the opportunity to handle it. If you have overlapping views this can be a sibling view. If not it will bubble up to the parent.

So when you return true , you have told android that you have handled all events, and it has nothing to do with. So you don't see any ripple effects even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM