代码之家  ›  专栏  ›  技术社区  ›  Mac Tíre

我正在尝试在我的应用程序中创建一个带有特定日期的倒计时计时器

  •  0
  • Mac Tíre  · 技术社区  · 9 年前

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
    
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.project.MainActivity"
        android:id="@+id/drawer_layout">
    
        <LinearLayout
    
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:weightSum="1">
    
            <include
                layout="@layout/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/countdown"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Next Event:"
                android:textStyle="bold"
                android:textSize="25dp" />
        </LinearLayout>
    
        <android.support.design.widget.NavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/navigation_view"
            android:layout_gravity="start"
            app:menu="@menu/drawer_menu"
            app:headerLayout="@layout/navigation_drawer_header"
            ></android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    

        private TextView txtCountdownEvent;
        private Handler handler;
        private Runnable runnable;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            txtCountdownEvent = (TextView) findViewById(R.id.countdown);
    
            actionBarDrawerToggle = new
    
                    ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.draw_close);
    
            drawerLayout.addDrawerListener(actionBarDrawerToggle);
    
            countDownStart();
        }
    
    
    
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            actionBarDrawerToggle.syncState();
    
        }
        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");
                        }
    // 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;
                            txtCountdownEvent.setText("" + String.format("%02d", days, hours, minutes, seconds));
    
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            handler.postDelayed(runnable, 1 * 1000);
    

    是因为我无法在一个文本视图中显示天、小时、分钟和秒吗。我遇到的问题是,当我运行应用程序时,计时器不会显示在应用程序上。非常感谢您的帮助。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Ephraim Kigamba    9 年前

    这个 dateFormat 变量为空

    您尚未创建的实例 SimpleDateFormat 对于 Android版本N以下的设备 try{ } catch () {}

    更正此问题的示例代码:

    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");
    }