自學(xué)鴻蒙應(yīng)用開發(fā)(8)- DatePicker組件
本文介紹在鴻蒙應(yīng)用中DatePicker組件的基本用法。
增加DatePicker組件
如下代碼中46行~51行所示,在布局中增加DatePicker組件。
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Componentohos:height="0vp"ohos:weight="3"ohos:width="match_parent"/><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="center"ohos:orientation="vertical"><Imageohos:id="$+id:image"ohos:width="match_content"ohos:height="match_content"ohos:layout_alignment="center"ohos:image_src="$media:DevEco"/><TextFieldohos:id="$+id:text_field"ohos:width="match_parent"ohos:height="30vp"ohos:text_size="20fp"ohos:text_alignment="center"ohos:hint="Please input text and press [Click me!] button."ohos:background_element="$graphic:background_text_field"/><Buttonohos:id="$+id:hello_button"ohos:width="match_content"ohos:height="match_content"ohos:text_size="27fp"ohos:text="Click me!"ohos:layout_alignment="center"ohos:background_element="$graphic:background_button"ohos:margin="15vp"ohos:right_padding="8vp"ohos:left_padding="8vp"/><DatePickerohos:id="$+id:date_pick"ohos:height="match_content"ohos:width="300vp"ohos:text_size="20fp"ohos:selected_text_size="40fp">DatePicker>DirectionalLayout><Componentohos:height="0vp"ohos:weight="5"ohos:width="match_parent"/>DirectionalLayout>
代碼中組件id被指定為date_pick會在下面的響應(yīng)代碼中用到。
在代碼中使用DatePicker組件
如下面代碼中21行和50行所示,在獲取DatePicker組件后,一方面在button的動作響應(yīng)中計算所選日期和當(dāng)前日期的差值之后用小窗口表示出來;另一方面在用戶操作DatePicker時將選擇結(jié)果表示在TextFile組件上。
package com.example.helloharmony.slice;import com.example.helloharmony.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.DatePicker;import ohos.agp.components.TextField;import ohos.agp.window.dialog.ToastDialog;import java.time.LocalDate;public class ComponentAbilitySlice extends AbilitySlice {public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_component);//獲取textfield輸入組件TextField tf = (TextField) findComponentById(ResourceTable.Id_text_field);//獲取button組件Button button = (Button) findComponentById(ResourceTable.Id_hello_button);//獲取datepicker組件DatePicker picker = (DatePicker) findComponentById(ResourceTable.Id_date_pick);// 為按鈕設(shè)置點(diǎn)擊事件回調(diào)button.setClickedListener(new Component.ClickedListener() {public void onClick(Component v) {LocalDate rightNow = LocalDate.now();LocalDate pickDate = LocalDate.of(picker.getYear(), picker.getMonth(), picker.getDayOfMonth());String msg;if(pickDate.isBefore(rightNow))msg = "所選日期比今天早" + pickDate.until(LocalDate.now()).getDays() + "天";else if(pickDate.isAfter(rightNow))msg = "所選日期比今天晚" + LocalDate.now().until(pickDate).getDays() + "天";elsemsg = "所選日期和今天是同一天";new ToastDialog(getContext()).setText(msg).show();}});//設(shè)定事件響應(yīng)picker.setValueChangedListener(new DatePicker.ValueChangedListener() {public void onValueChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {tf.setText(year+"/"+monthOfYear+"/"+dayOfMonth);}});}public void onActive() {super.onActive();}public void onForeground(Intent intent) {super.onForeground(intent);}}
畫面顯示如下:
參考文檔
DatePicker類:
https://developer.harmonyos.com/cn/docs/documentation/doc-references/datepicker-0000001054678716
DatePicker組件:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-datepicker-0000001060237839
新書介紹
《實(shí)戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個設(shè)計模式的運(yùn)用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運(yùn)用設(shè)計模式。
對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>

