【问题标题】:after the countdown timer is finished send user to the new activity?倒数计时器完成后将用户发送到新活动?
【发布时间】:2018-06-15 05:16:22
【问题描述】:

我想在倒数计时器完成后将用户 AbcActivity 发送到 XyzActivity。像这样我在 AbcActivity 上设置倒数计时器 5 分钟,当 倒数计时器将为 0 或完成它会自动将用户 AbcActivity 发送到 XyzActivity。

我该怎么做?

`

    txtDay = (TextView) findViewById(R.id.txtDay);
    txtHour = (TextView) findViewById(R.id.txtHour);
    txtMinute = (TextView) findViewById(R.id.txtMinute);
    txtSecond = (TextView) findViewById(R.id.txtSecond);
    tvEventStart = (TextView) findViewById(R.id.tveventStart);
    mButton= (Button) findViewById(R.id.button);
   mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent a= new Intent(Start.this,Timetable.class);
            startActivity(a);
        }
    });

    countDownStart();
}
public void countDownStart() {
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd");
                // Please here set your event date//YYYY-MM-DD
                Date futureDate = dateFormat.parse("2018-11-4");
                Date currentDate = new Date();
                if (!currentDate.after(futureDate)) {
                    long diff = futureDate.getTime()
                            - currentDate.getTime();
                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days * (24 * 60 * 60 * 1000);
                    long hours = diff / (60 * 60 * 1000);
                    diff -= hours * (60 * 60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60 * 1000);
                    long seconds = diff / 1000;
                    txtDay.setText("" + String.format("%02d", days));
                    txtHour.setText("" + String.format("%02d", hours));
                    txtMinute.setText(""
                            + String.format("%02d", minutes));
                    txtSecond.setText(""
                            + String.format("%02d", seconds));
                } else {
                    tvEventStart.setVisibility(View.VISIBLE);
                    tvEventStart.setText("The event started!");
                    textViewGone();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 1 * 1000);
}

public void textViewGone() {
    findViewById(R.id.LinearLayout1).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout2).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout3).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout4).setVisibility(View.GONE);
    findViewById(R.id.textViewheader1).setVisibility(View.GONE);

}

}

`

【问题讨论】:

    标签: java android android-studio countdown


    【解决方案1】:

    您可以简单地使用 android 中的 CountDownTimer 类来实现此功能。您可以在计时器对象的 onFinish() 方法中启动 xyzActivity

    【讨论】:

      【解决方案2】:
          new CountDownTimer(300000, 1000) {
      
              public void onTick(long millisUntilFinished) {
                  mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
              }
      
              public void onFinish() {
                  mTextField.setText("done!");
                  Intent intent = new Intent(MainActivity.this,NewActivity.class);
                  startActivity(intent);
              }
          }.start();
      

      【讨论】:

      • 你能在我的代码中添加这个吗?我不明白在哪里添加此代码。
      【解决方案3】:

      请更新您的 countDownStart 方法

      public void countDownStart() {
          CountDownTimer countDownTimer = new CountDownTimer(300000, 1000) {
              @Override
              public void onTick(long l) {
                  // every second, this method will be call
              }
      
              @Override
              public void onFinish() {
                  // after 5 minutes, this method will be call
                  tvEventStart.setVisibility(View.VISIBLE);
                  tvEventStart.setText("The event started!");
                  textViewGone();
              }
          };
      }
      

      【讨论】:

        【解决方案4】:
            int total;
            long interval;
            private int temp;    
        // declare before starting CountDownTimer
                total = 3600 * hrs + 60 * min + sec;// outcome will be in seconds
                interval = total * 1000;// outcome will be in milliseconds
        
        CountDownTimer countDownTimer = new CountDownTimer(interval, 1000) {
        int hours, minutes, seconds;
                        @Override
                        public void onTick(long millisUntilFinished) {
                            // when countdown counts
                        temp = total - 1;
                        hours = temp / 3600;
                        temp = temp - 3600 * hours;
                        minutes = temp / 60;
                        temp = temp - 60 * minutes;
                        seconds = temp;
                        total = 3600 * hours + 60 * minutes + seconds;
        
                        txtHour.setText("" + String.format("%02d", hours));
                        txtMinute.setText(""
                                + String.format("%02d", minutes));
                        txtSecond.setText(""
                                + String.format("%02d", seconds));
                        }
        
                        @Override
                        public void onFinish() {
                            // when the countdown completed intent to another application
                        }
                    }.start();
        

        【讨论】:

        • 你能在我的代码中添加这个吗?我不明白在哪里添加此代码。
        • } else { tvEventStart.setVisibility(View.VISIBLE); tvEventStart.setText("活动开始!"); textViewGone();这里可以做点什么吗?
        • 我已经测试并更新了答案。请检查并返回
        【解决方案5】:

        您可以在此处添加到您的代码中

        if (!currentDate.after(futureDate)) {
                        long diff = futureDate.getTime()
                                - currentDate.getTime();
                        long days = diff / (24 * 60 * 60 * 1000);
                        diff -= days * (24 * 60 * 60 * 1000);
                        long hours = diff / (60 * 60 * 1000);
                        diff -= hours * (60 * 60 * 1000);
                        long minutes = diff / (60 * 1000);
                        diff -= minutes * (60 * 1000);
                        long seconds = diff / 1000;
                        txtDay.setText("" + String.format("%02d", days));
                        txtHour.setText("" + String.format("%02d", hours));
                        txtMinute.setText(""
                                + String.format("%02d", minutes));
                        txtSecond.setText(""
                                + String.format("%02d", seconds));
        
                     if (days <= 0) {
                                if (hours <= 0) {
                                    if (minutes <= 0) {
                                        if (seconds <= 0) {
                                            // put your intent here
                                        }
                                    }
                                }
                            }
        
        
                    } 
        

        【讨论】:

        • 我做不到,你能给我完整的代码吗?我无法添加它所以我出错请给我例子
        • 你需要什么?在代码 sn-p Intent intent = new Intent(AbcActivity.this, XyzActivity.class); startactivity(intent); 中只放你的评论
        • 您应该与您的代码进行比较,因为我在您的代码中添加了一些部分,您可以清楚地显示我添加代码的位置。因此您可以轻松获得解决方案
        猜你喜欢
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多