简体   繁体   中英

How to get button color back to normal after a simulated click

In my Android app I simulate a button click with the following code :

  void clickButton(final Button b, int delay)
  {

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
      public void run()
      {
        final Drawable drawable = b.getBackground();
        b.performClick();
        b.getBackground().setColorFilter(b.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
//        b.setBackgroundColor(Color.rgb(88, 166, 198));
        b.setPressed(true);
        b.invalidate();
        // delay completion till animation completes
        b.postDelayed(new Runnable() {  //delay button
          public void run() {
            b.setPressed(false);
            b.invalidate();
            b.setBackground(drawable);
            //any other associated action
          }
        }, 800);  // .8secs delay time
      }
    }, delay);
  }

But the color of the button will stay green after a click, how to get it back to the color before the click, after a .5 sec delay?

b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.colorAccent));
b.postDelayed(new Runnable() {
    public void run() {
        b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.initialColor));
    }
}, 800);

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