下面是當我運行應用程序時,我的倒數計時器顯示在文本視圖中的代碼。但是,當我運行它時,它不會出現。我正在嘗試創建倒數計時器,其中包含事件的日,時,分和秒?
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"/>
所以它工作?任何想法爲什麼它不出現在我的文本視圖? –