繁体   English   中英

模拟点击后如何使按钮颜色恢复正常

[英]How to get button color back to normal after a simulated click

在我的 Android 应用程序中,我使用以下代码模拟按钮单击:

  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);
  }

但是单击后按钮的颜色将保持绿色,如何在 0.5 秒延迟后使其恢复到单击前的颜色?

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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