?Android?,?????????????????,?????CountDownTimer
????????????,????????????,????????????:
-
???????: ???
AndroidManifest.xml
???,??????(????): -
??????: ???
res/layout
???,????????(??activity_main.xml
),?????AnalogClock
???TextView
??????: -
???????: ???
MainActivity.java
???,???????:import android.os.Bundle; import android.os.CountDownTimer; import android.widget.AnalogClock; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private AnalogClock analogClock; private TextView timerTextView; private CountDownTimer countDownTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); analogClock = findViewById(R.id.analogClock); timerTextView = findViewById(R.id.timerTextView); // ?????????(??,?????????10?) long endTimeMillis = System.currentTimeMillis() + 10000; // ????? countDownTimer = new CountDownTimer(endTimeMillis, 1000) { @Override public void onTick(long millisUntilFinished) { // ??????? long seconds = (millisUntilFinished / 1000) % 60; timerTextView.setText(String.format("d:d", seconds / 60, seconds % 60)); } @Override public void onFinish() { // ????????? timerTextView.setText("00:00"); } }.start(); } @Override protected void onDestroy() { super.onDestroy(); // ??????,????? if (countDownTimer != null) { countDownTimer.cancel(); } } }
??????,???????CountDownTimer
??,??????????10??onTick
??????????????,?onFinish
??????????????
???,?????????????,?????????????????,?????????????????????,????????????????