我又有疑问......我怎样才能解决这个日期倒计时问题?每当我启动应用程序时,它都会打印完成。如果我在代码未来日期写,app打印它也完成了。你能帮助我吗?
package lt.sviesioji.kdainiviesiojigimnazija;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
/**
* A simple {@link Fragment} subclass.
*/
public class PagrindinisFragment extends Fragment {
public PagrindinisFragment() {
// Required empty public constructor
}
Button k,f;
TextView tv_countdown;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_pagrindinis, container,
false);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Kėdainių šviesioji gimnazija");
tv_countdown = (TextView) rootView.findViewById(R.id.textView17);
Calendar start_calendar = Calendar.getInstance();
Calendar end_calendar = Calendar.getInstance();
long start_millis = start_calendar.getTimeInMillis(); //get the start time in milliseconds
long end_millis = end_calendar.getTimeInMillis(); //get the end time in milliseconds
long total_millis = (end_millis - start_millis); //total time in milliseconds
end_calendar.set(2005, 07, 1); // 10 = November, month start at 0 = January
//1000 = 1 second interval
CountDownTimer cdt = new CountDownTimer(total_millis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
tv_countdown.setText(days + ":" + hours + ":" + minutes + ":" + seconds); //You can compute the millisUntilFinished on hours/minutes/seconds
}
@Override
public void onFinish() {
tv_countdown.setText("Finish!");
}
};
cdt.start();
k = (Button) rootView.findViewById(R.id.button2);
k.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment newFragment = new KalendoriusFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
f = (Button) rootView.findViewById(R.id.button3);
f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment newFragment = new FormulynasFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView;
}
}
答案 0 :(得分:0)
您在更改total_millis
的日期之前计算end_calendar
。这意味着total_millis
将始终非常接近0
。要解决此问题,请在计算差异之前设置end_calendar
的数据。这是一个例子:
Calendar start_calendar = Calendar.getInstance();
Calendar end_calendar = Calendar.getInstance();
end_calendar.set(2016, 07, 1); // 10 = November, month start at 0 = January
long start_millis = start_calendar.getTimeInMillis(); //get the start time in milliseconds
long end_millis = end_calendar.getTimeInMillis(); //get the end time in milliseconds
long total_millis = (end_millis - start_millis); //total time in milliseconds
我已将年份更改为2016
(将来的某个日期),以便将total_millis
设置为正数。