Follow me Android 9 date and time components

Video lesson: https://edu.csdn.net/course/play/7621

In this chapter

Section 1 AnalogClock and DigitalClock
Section 2 CalendarView
Section 3 DatePicker and TimerPicker
Section 4 Chronometer
Section 5 Timer Class

Objective of this chapter

Master the usage of graphic clock and digital clock.
Master the usage of calendar view.
Familiar with the usage of date and time selector.
Familiar with the usage of Chronometer.
Familiar with the usage of Timer class.


AnalogClock is an analog clock view. It uses the AnalogClock label for layout. In addition to length and width, other attributes are basically required. The corresponding Java class is android.widget.AnalogClock. The layout example is as follows: 



<AnalogClockandroid:layout_width="wrap_content"android:layout_height="wrap_content" />





DigitalCloc k is a digital clock view. It uses the DigitalClock label for layout. It basically needs other attributes besides length and width. The corresponding Java class is android.widget.DigitalClock. The layout example is as follows:


<DigitalClockandroid:layout_width="wrap_content"android:layout_height="wrap_content" />


 



CalendarView , can be used to display and select dates


<CalendarView		android:layout_width="match_parent"		android:layout_height="match_parent"		android:firstDayOfWeek="3"		android:shownWeekCount="4"		android:selectedWeekBackgroundColor="#aff"		android:focusedMonthDateColor="#f00"		android:weekSeparatorLineColor="#ff0"		android:unfocusedMonthDateColor="#f9f"		android:id="@+id/calendarView" />


Event, option change event 



public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {// Use Toast to display the date selected by the user Toast.makeText(CalendarViewTest.this, "Your birthday is" + year + "year" + month + "month "+ dayOfMonth + "日", Toast.LENGTH_SHORT).show(); }});






DatePicker is a control for date selection, using DatePicker for layout, the common attributes are as follows:
android:calendarViewShown indicates whether to display the full calendar
android:endYear indicates the maximum year that can be selected
android:maxDate indicates the maximum date displayed in the calendar
android:spinnerShown indicates whether Show adjustment arrow buttons

    


DatePicker is a control used for date selection. The corresponding class is android.widget.DatePicker. Common methods are as follows:

init() is used to initialize the listener for display date and registration date selection changes

getYear() is used to extract the selected year

getMonth() is used to extract the selected month

getDayOfMonth() is used to extract the selected day

The layout example is as follows:


<DatePicker android:id="@+id/datepick"android:layout_width="wrap_content"android:layout_height="wrap_content"android:calendarViewShown="true"/>


TimePicker is a control for time selection, using TimePicker for layout, basically no other attributes need to be set except for length and width 


The corresponding class is android.widget.TimePicker, and the common methods are as follows:

getCurrentHour() Get the current hour

getCurrentMinute() Get the current minute

android.widget.TimePicker.OnTimeChangedListener is the event listening interface



Chronometer , this component and DigitalClock all inherit from TextView, so they both display a piece of text. However, the Chronometer does not display the current time, it shows how much time has passed since a certain start time.
Chronometer supports the following common methods.
setBase(long base): Set the start time of the timer.
setFormat(String format): Set the format of the display time.
start(): Start timing.
stop(): Stop timing.
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener): Bind an event listener to the timer, which is triggered when the timer changes.


Use the Chronometer control to realize the operation of the counter, start

// Set the start timing time   
                chronometer.setBase(SystemClock.elapsedRealtime());   
                // Start timing   
                chronometer.start();   

chronometer.stop(); //Stop 


Event triggered by timer



chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() (public void onChronometerTick(Chronometer chronometer) {// If the timing of the start has exceeded the timer seconds if (SystemClock.elapsedRealtime()- chronometer.getBase()> startTime * 1000) (chronometer .stop();// Prompt the user to showDialog();})));




Make slides based on Chronometer




Timer class
Timer class function is similar to Chronometer component, it can execute program code every specific time, and its function is stronger than Chronometer component.
Syntax



The Timer object is executed in the schedule method

Timer object name.schedule (TimerTask object, delay time, interval time);


TimerTask object : It is the program code that the Timer object executes regularly. Developers must write the program code to be executed by themselves. Delay time: Set how long it takes to execute the Timer object, in milliseconds. Interval: Set how often the TimerTask object is executed once, in milliseconds.


The TimerTask
object of the TimerTask class is the main body of the Timer object, which is used to define the content of the work to be executed by the Timer object. The program code in the run method in the TimerTask object is a code block that is repeatedly executed, the syntax is



private TimerTask variable name=new TimerTask() {public void run(){execute program code...});


Handler对象是应用程序中不同线程之间的消息中介,在TimerTask对象中使用Message对象送出消息。 



public void run (){Message message=new Message () ;message.what=送出消息;handler.sendMessage (message) ;}


Handler对象接收消息的语法为: 




private Handler变量名称=new  Handler(){public void handleMessage(Message msg    {    super.handleMessage (msg);    	switch (msg.what){    	case接收消息: 	程序代码    	break;)   }};

计时秒表初始化计时器




Timer timer=new Timer();//创建Timer对象timer.schedule(new TimerTask() {//创建TimerTask对象public void run() {	if(flag){	tSec++;	Message msg=new Message();//创建消息对象	msg.what=1;//设定类型	handler.sendMessage(msg);//传送消息给Handler	}}}, 0,1000);//立刻开始执行,时间间隔为1000毫秒


计时秒表,接受消息 



public void handleMessage(Message msg){//接受消息super.handleMessage(msg);switch(msg.what){//判断消息类型	case 1:		cSec=tSec%60;//获取秒数		cMin=tSec/60;//获取分钟数	String str=String.format("%02d:%02d", cMin,cSec);//以00:00的格式显示数据	txtClock.setText(str);	break;}}





Guess you like

Origin blog.51cto.com/2096101/2588815