【问题标题】:I need to change the colour of a button in an android widget我需要更改 android 小部件中按钮的颜色
【发布时间】:2020-02-14 17:36:45
【问题描述】:

我有一个带有按钮的小部件,用户必须在给定的时间间隔内按下该按钮。该按钮工作正常并重置间隔,但我希望按钮根据剩余时间改变颜色绿色->琥珀色->红色。使用远程视图更改按钮上的文本没有问题,代码如下:

RemoteViews views =new RemoteViews(context.getPackageName(), R.layout.example_widget);
views.setCharSequence(R.id.example_widget_button, "setText", buttonText);

但我无法让任何类型的代码更改按钮颜色。我已经尝试了几件事:

views.setCharSequence(R.id.example_widget_button, "setBackgroundTint", "#039be5");

我也尝试过使用可绘制背景并对其进行更改。我遗漏了一些非常明显的东西 - 这一定是可能的 - 我只是找不到在我的上下文中有效的例子。

谁能指点我?

【问题讨论】:

  • Mohammad:谢谢你——它有效,而且它开辟了一堆我可以尝试的其他东西。 Mehmed:也谢谢 - 我已经设置并运行了计时器元素,但你也让我对此深思熟虑; Runnable 和 Handler 对我来说是新的。我知道不应该以这种方式使用 cmets,但我不能投票所以这里是:O)

标签: android button colors widget


【解决方案1】:

你可以这样做:

views.setInt(R.id.example_widget_button, "setBackgroundColor", android.graphics.Color.BLACK)`;

然后您将颜色更改为当时想要的颜色。

【讨论】:

    【解决方案2】:

    您可以使用HandlerCountDownTimer

    如果你想使用Handler,这里是例子:

    long totalTime = 10000;
    long warningTime = 6000;
    long alertTime = 30000;
    
    Runnable warningColorChangeRunnable = new Runnable() {
        @Override
        public void run() {
            button.setBackgroundColor(getResources().getColor(R.color.colorWarning));
        }
    };
    
    Runnable alertColorChangeRunnable = new Runnable() {
        @Override
        public void run() {
            button.setBackgroundColor(getResources().getColor(R.color.colorAlert));
        }
    };
    
    final Handler handler = new Handler();
    handler.postDelayed(warningColorChangeRunnable, totalTime - warningTime);
    handler.postDelayed(alertColorChangeRunnable, totalTime - alertTime);
    
    button.setOnClickListener(new OnClickListener() {
        @Override public void onClick(View view) {
            handler.removeCallbacks(warningColorChangeRunnable);
            handler.removeCallbacks(alertColorChangeRunnable);
        }
    });
    

    如果你想使用CountDownTimer,这里是例子:

    long totalTime = 10000;
    long warningTime = 6000;
    long alertTime = 30000;
    long interval = 1000;
    
    CountDownTimer timer = new CountDownTimer(totalTime, 1000) {
    
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished <= warningTime && millisUntilFinished > warningTime - interval) {
                button.setBackgroundColor(getResources().getColor(R.color.colorWarning));
            }
            if (millisUntilFinished <= alertTime && millisUntilFinished > alertTime - interval) {
                button.setBackgroundColor(getResources().getColor(R.color.colorAlert));
            }
        }
    
        public void onFinish() {
            // Maybe show a failure dialog
        }
    }.start();
    
    button.setOnClickListener(new OnClickListener() {
        @Override public void onClick(View view) {
            timer.cancel();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多