【问题标题】:I am trying to create a countdown timer which includes days, hours, minutes and seconds of an event?我正在尝试创建一个倒数计时器,其中包括事件的天、小时、分钟和秒?
【发布时间】:2017-07-26 23:55:33
【问题描述】:

以下是我在运行应用程序时将倒数计时器显示到文本视图中的代码。但是,当我运行它时,它不会出现。

private TextView countdowndisplay;
private Handler handler;
private Runnable runnable;

countdowndisplay = (TextView) view.findViewById(R.id.countdowntv);

        countDownStart();



 public void countDownStart() {
        handler = new Handler();
        runnable = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void run() {
                handler.postDelayed(this, 1000);
                try {
                    SimpleDateFormat dateFormat = null;
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    } else {
                        //Set the optional Date format here for Devices Running ANDROID VERSION BELOW N
                        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    }
// Please here set your event date//YYYY-MM-DD
                    Date futureDate = dateFormat.parse("2017-09-23");
                    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;
                        countdowndisplay.setText("" + String.format("%02d", days, hours, minutes, seconds));

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, 1 * 1000);

    }

下面是我想要显示输出的 textView 的 XML。

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/nextdrifttv"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/countdowntv"/>

【问题讨论】:

    标签: java android timer countdown


    【解决方案1】:

    我会以不同的方式执行此操作,但这可以使用您的代码 - android。

    package com.example.coundown.countdownapp;
    
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.annotation.RequiresApi;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class MainActivity extends AppCompatActivity {
        private TextView countdowndisplay;
        private Handler handler;
        private Runnable runnable;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            countdowndisplay = (TextView) findViewById(R.id.coundowntimer);
    
            countDownStart();
    
        }
    
        public void countDownStart() {
            handler = new Handler();
            runnable = new Runnable() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void run() {
                    handler.postDelayed(this, 1000);
                    try {
                        SimpleDateFormat dateFormat = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        } else {
                            //Set the optional Date format here for Devices Running ANDROID VERSION BELOW N
                            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        }
    // Please here set your event date//YYYY-MM-DD
                        Date futureDate = dateFormat.parse("2017-09-23");
                        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;
                            if (countdowndisplay != null)
                                countdowndisplay.setText(String.valueOf(days) + " days \n" + String.valueOf(hours) + " hours \n " + String.valueOf(minutes) + " minutes \n" + String.valueOf(seconds)+ " seconds");
    
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            handler.postDelayed(runnable, 1 * 1000);
    
        }
    
    
    }
    

    【讨论】:

    • 所以它有效吗?任何想法为什么它没有出现在我的文本视图中?
    【解决方案2】:

    试试这个。您的问题是从可运行更新 UI。

    public class MainActivity extends AppCompatActivity {
         TextView countdowndisplay;
        int count=0;
        Handler handler = new Handler();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
            countdowndisplay = (TextView) findViewById(R.id.mainText);
            countdowndisplay.setText("really");
            countDownStart();
    
        }
        void countDownStart(){
            //Post handler after 1 second.
            handler.postDelayed(runnable, 1000);
        }
    
    
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                count++;
                handler.postDelayed(runnable, 1000);
                ///**Update your UI from here**///
                countdowndisplay.setText(count+" Second");
            }
        };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-15
      • 2019-11-24
      • 2020-09-15
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多