【问题标题】:how to change backgrounds color of cardview day wise?如何明智地更改cardview的背景颜色?
【发布时间】:2020-04-19 06:05:31
【问题描述】:

我正在开发一个应用程序。在该应用程序中,有一个卡片视图。我想每天更改卡片视图的背景颜色。

喜欢 星期一 = 红色 星期二 = 绿色

直到周日!从下周一开始,它会从第一个开始,比如 Monday=red color

有什么想法吗?如何做到这一点?

我试过了

Calendar calendar = Calendar.getInstance();
        String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

它给了我这样的输出

Sunday, April 19, 2020

现在,如何使用它来更改背景颜色?

【问题讨论】:

  • 请确保向我们展示您的尝试
  • 我什至不知道如何实现它,因为我是一个初学者。我试图在这里搜索,但没有找到任何资源。任何小想法都会帮助我实现这一目标
  • 请与我们分享您尝试过的内容,您所解释的内容,使用 switch case 很容易实现并将数字设置为每天,但为了更清楚,请与我们分享您的代码。
  • 您能否附上您想要实现的目标的图片?

标签: android android-studio android-calendar


【解决方案1】:

使用SimpleDateFormat 将日期和时间格式化为人类可读的字符串,相对于用户的区域设置。

获取当前星期几的示例(例如“星期日”):

Calendar c= Calendar.getInstance();
SimpleDateFormat sd=new SimpleDateFormat("EEEE");
String dayofweek=sd.format(c.getTime());

现在改变背景颜色:

if (dayofweek.equals("Saturday")) {
    cardview.setBackgroundColor(getResources().getColor(color.white));
}

【讨论】:

    【解决方案2】:

    int dayOfWeek=bday.get(Calendar.DAY_OF_WEEK); // 返回 3,表示星期二

    然后使用 if ,else if 语句对应不同日期的不同颜色。

    【讨论】:

      【解决方案3】:

      你可以用下面的代码来实现

      Calendar calendar = Calendar.getInstance();
      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      
      
      switch (dayOfWeek) {
        case Calendar.MONDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_monday));
          break;
        case Calendar.TUESDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_tuesday));
          break;
        case Calendar.WEDNESDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_wednesday));
          break;
        case Calendar.THURSDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_thursday));
          break;
        case Calendar.FRIDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_friday));
          break;
        case Calendar.SATURDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_saturday));
          break;
        case Calendar.SUNDAY:
          cardView.setBackground(ContextCompat.getColor(context, R.color.color_sunday));
          break;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        • 2019-02-08
        • 1970-01-01
        • 2011-03-29
        • 1970-01-01
        • 2014-12-21
        相关资源
        最近更新 更多