How to set button click effect in Android?

后端 未结 14 2379
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 01:29

In Android, when I set background image to Button, I can not see any effect on button.

I need some effect on button so user can recognize that button is clicked.

14条回答
  •  甜味超标
    2020-11-28 01:58

    Making a minor addition to Andràs answer:

    You can use postDelayed to make the color filter last for a small period of time to make it more noticeable:

            @Override
            public boolean onTouch(final View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        v.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                        v.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        v.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                v.getBackground().clearColorFilter();
                                v.invalidate();
                            }
                        }, 100L);
                        break;
                    }
                }
                return false;
            }
    

    You can change the value of the delay 100L to suit your needs.

提交回复
热议问题