Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用
Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用
簡(jiǎn)單復(fù)習(xí)下基礎(chǔ)UI組件,做個(gè)簡(jiǎn)單的總結(jié),Android的這些組件封裝的特別好,基本套上就能使用,當(dāng)然,這個(gè)減輕了開(kāi)發(fā)者的負(fù)擔(dān)!不過(guò)如果想要深入研究,這里面還是有很大的空間值得深度分析!簡(jiǎn)單的幾個(gè)例子!僅供參考:
不多說(shuō),先上效果圖:
CalendarView
ChooseView
NumberPicker
CalendarView代碼區(qū) :
main.xml代碼區(qū):CalendarView組件的使用加上一些簡(jiǎn)單的屬性即可!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇您的生日:"/> <!-- 設(shè)置以星期二作為每周第一天 設(shè)置該組件總共顯示4個(gè)星期 并對(duì)該組件的日期時(shí)間進(jìn)行了定制 --> <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" /> </LinearLayout>
Activity區(qū)代碼:
public class MainActivity extends Activity { CalendarView cv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cv = (CalendarView)findViewById(R.id.calendarView); // 為CalendarView組件的日期改變事件添加事件監(jiān)聽(tīng)器 cv.setOnDateChangeListener(new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { // 使用Toast顯示用戶(hù)選擇的日期 Toast.makeText(MainActivity.this, "你生日是" + year + "年" + month + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show(); } }); } }
DatePicker,TimePicker,結(jié)合Calerdar的使用,可以供用戶(hù)選擇日期時(shí)使用:
代碼區(qū):
main.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇購(gòu)買(mǎi)本書(shū)的具體時(shí)間"/> <!-- 定義一個(gè)DatePicker組件 --> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:startYear="2000" android:endYear="2016" android:calendarViewShown="true" android:spinnersShown="true"/> <!-- 定義一個(gè)TimePicker組件 --> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center_horizontal"/> <!-- 顯示用戶(hù)輸入日期、時(shí)間的控件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> </LinearLayout>
Activity代碼:
public class MainActivity extends Activity { // 定義5個(gè)記錄當(dāng)前時(shí)間的變量 private int year; private int month; private int day; private int hour; private int minute; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker); TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker); // 獲取當(dāng)前的年、月、日、小時(shí)、分鐘 Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR); minute = c.get(Calendar.MINUTE); // 初始化DatePicker組件,初始化時(shí)指定監(jiān)聽(tīng)器 datePicker.init(year, month, day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker arg0, int year , int month, int day) { MainActivity.this.year = year; MainActivity.this.month = month; MainActivity.this.day = day; // 顯示當(dāng)前日期、時(shí)間 showDate(year, month, day, hour, minute); } }); timePicker.setEnabled(true); // 為T(mén)imePicker指定監(jiān)聽(tīng)器 timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view , int hourOfDay, int minute) { MainActivity.this.hour = hourOfDay; MainActivity.this.minute = minute; // 顯示當(dāng)前日期、時(shí)間 showDate(year, month, day, hour, minute); } }); } // 定義在EditText中顯示當(dāng)前日期、時(shí)間的方法 private void showDate(int year, int month , int day, int hour, int minute) { EditText show = (EditText) findViewById(R.id.show); show.setText("您的購(gòu)買(mǎi)日期為:" + year + "年" + (month + 1) + "月" + day + "日 " + hour + "時(shí)" + minute + "分"); } }
NumberPicker主要使用在給用戶(hù)提供數(shù)字選擇時(shí)使用。
main.xml代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="選擇低價(jià):" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np1" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="選擇高價(jià):" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np2" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> </TableLayout>
Activity代碼:
public class MainActivity extends Activity { NumberPicker np1, np2; // 定義最低價(jià)格、最高價(jià)格的初始值 int minPrice = 25, maxPrice = 75; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); np1 = (NumberPicker) findViewById(R.id.np1); // 設(shè)置np1的最小值和最大值 np1.setMinValue(10); np1.setMaxValue(50); // 設(shè)置np1的當(dāng)前值 np1.setValue(minPrice); np1.setOnValueChangedListener(new OnValueChangeListener() { // 當(dāng)NumberPicker的值發(fā)生改變時(shí),將會(huì)激發(fā)該方法 @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { minPrice = newVal; showSelectedPrice(); } }); np2 = (NumberPicker) findViewById(R.id.np2); // 設(shè)置np2的最小值和最大值 np2.setMinValue(60); np2.setMaxValue(100); // 設(shè)置np2的當(dāng)前值 np2.setValue(maxPrice); np2.setOnValueChangedListener(new OnValueChangeListener() { // 當(dāng)NumberPicker的值發(fā)生改變時(shí),將會(huì)激發(fā)該方法 @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { maxPrice = newVal; showSelectedPrice(); } }); } private void showSelectedPrice() { Toast.makeText(this, "您選擇最低價(jià)格為:" + minPrice + ",最高價(jià)格為:" + maxPrice, Toast.LENGTH_SHORT) .show(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
- Android開(kāi)發(fā)之DatePickerDialog、TimePickerDialog時(shí)間日期對(duì)話框用法示例
- Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
- Android TimePicker 直接輸入的問(wèn)題解決方案
- Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法分析
- Android時(shí)間對(duì)話框TimePickerDialog詳解
- Android開(kāi)發(fā)之TimePicker控件用法實(shí)例詳解
- android中DatePicker和TimePicker的使用方法詳解
- Android日歷控件PickTime代碼實(shí)例
相關(guān)文章
Android進(jìn)程間通信(IPC)機(jī)制Binder簡(jiǎn)要介紹
本文主要介紹 Android進(jìn)程間通信(IPC)機(jī)制Binder簡(jiǎn)要介紹, 這里介紹了Binder機(jī)制如何實(shí)現(xiàn)進(jìn)程通信機(jī)制,有研究Android源碼的朋友可以看下2016-08-08Win8下Android SDK安裝與環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Win8下Android SDK安裝與環(huán)境變量配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android開(kāi)發(fā)之基于RecycleView實(shí)現(xiàn)的頭部懸浮控件
RecyclerView是一種類(lèi)似于ListView的一個(gè)滑動(dòng)列表,但是RecyclerView和ListView相比,RecyclerView比ListView更好,這篇文章重點(diǎn)給大家介紹基于RecycleView實(shí)現(xiàn)的頭部懸浮控件,感興趣的朋友一起看看吧2019-10-10Android 實(shí)現(xiàn)IOS 滾輪選擇控件的實(shí)例(源碼下載)
這篇文章主要介紹了 Android 實(shí)現(xiàn)IOS 滾輪選擇控件的實(shí)例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-03-03Android中post請(qǐng)求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例
下面小編就為大家分享一篇Android中post請(qǐng)求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01簡(jiǎn)單談?wù)刟ndroid studio 的單元測(cè)試
昨天在完善項(xiàng)目的時(shí)候,需要進(jìn)行單元測(cè)試,在Eclipse環(huán)境中進(jìn)行是很簡(jiǎn)單的,但是在Android Studio環(huán)境中進(jìn)行單元測(cè)試,在國(guó)內(nèi)找了很多資料,大都是人云亦云,本文發(fā)布出來(lái)供大家學(xué)習(xí)參考。2016-08-08Kotlin ViewModelProvider.Factory的使用實(shí)例詳解
這篇文章主要介紹了Kotlin ViewModelProvider.Factory的使用,在我們使用 ViewModel 的時(shí)候,我們會(huì)發(fā)現(xiàn),有的時(shí)候我們需要用到 ViewModelFactory,有的時(shí)候不需要2023-02-02詳解Android studio實(shí)現(xiàn)語(yǔ)音轉(zhuǎn)文字功能
這篇文章主要介紹了如何通過(guò)Android studio調(diào)用科大訊飛的語(yǔ)音轉(zhuǎn)文字功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03Android啟動(dòng)內(nèi)置APK和動(dòng)態(tài)發(fā)送接收自定義廣播實(shí)例詳解
這篇文章主要介紹了Android啟動(dòng)內(nèi)置APK和動(dòng)態(tài)發(fā)送接收自定義廣播實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android RecyclerView 滾動(dòng)到中間位置的方法示例
這篇文章主要介紹了Android RecyclerView 滾動(dòng)到中間位置的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03