簡體   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