Android實(shí)現(xiàn)日期時(shí)間選擇對(duì)話框
日期/時(shí)間選擇對(duì)話框(DatePickerDialog和TimePickerDialog)的使用,供大家參考,具體內(nèi)容如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/edit" android:layout_width="200dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="日期選擇器" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="時(shí)間選擇器" /> </LinearLayout>
public class MainActivity extends Activity { // 實(shí)例化控件 private Button dateButton; private Button timeButton; private EditText editText; private DatePickerDialog dateDialog; private TimePickerDialog timeDialog; private int year, monthOfYear, dayOfMonth, hourOfDay, minute; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); // 通過findViewById找到控件 dateButton = (Button) findViewById(R.id.button1); timeButton = (Button) findViewById(R.id.button2); editText = (EditText) findViewById(R.id.edit); // 通過Calendar對(duì)象來獲取年、月、日、時(shí)、分的信息 Calendar calendar = Calendar.getInstance(); year = calendar.get(calendar.YEAR); monthOfYear = calendar.get(calendar.MONTH); dayOfMonth = calendar.get(calendar.DAY_OF_MONTH); hourOfDay = calendar.get(calendar.HOUR_OF_DAY); minute = calendar.get(calendar.MINUTE); /* * 實(shí)例化DatePickerDialog */ dateDialog = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) { // 把獲取的日期顯示在文本框內(nèi),月份從0開始計(jì)數(shù),所以要加1 String text = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth; editText.setText(text); } }, year, monthOfYear, dayOfMonth); // 后面的三個(gè)參數(shù)對(duì)應(yīng)于上面的年、月、日 /** * 對(duì)日期選擇器按鈕設(shè)置監(jiān)聽事件 */ dateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // 點(diǎn)擊日期選擇器按鈕時(shí)顯示出日期對(duì)話框 dateDialog.show(); } }); /* * 實(shí)例化TimePickerDialog */ timeDialog = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_LONG).show(); } }, hourOfDay, minute, true); // 最后一個(gè)參數(shù)設(shè)置是否為24小時(shí)制 /** * 對(duì)時(shí)間選擇器按鈕設(shè)置監(jiān)聽事件 */ timeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // 點(diǎn)擊時(shí)間選擇器按鈕時(shí)顯示出時(shí)間對(duì)話框 timeDialog.show(); } }); } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 自定義日期段選擇控件功能(開始時(shí)間-結(jié)束時(shí)間)
- Android開發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
- Android仿iPhone日期時(shí)間選擇器詳解
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
- Android開發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中TimePicker與DatePicker時(shí)間日期選擇組件的使用實(shí)例
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
相關(guān)文章
Android自定義Adapter的ListView的思路及代碼
Android自定義Adapter的ListView的思路及代碼,需要的朋友可以參考一下2013-05-05Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Android 仿淘寶、京東商品詳情頁向上拖動(dòng)查看圖文詳情控件DEMO詳解
本文給大家介紹android 仿淘寶、京東商品詳情頁向上拖動(dòng)查看圖文詳情控件DEMO詳解,使用兩個(gè)scrollView,兩個(gè)scrollView 豎直排列,通過自定義viewGroup來控制兩個(gè)scrollView的豎直排列,以及滑動(dòng)事件的處理。對(duì)android 拖動(dòng)查看圖文詳情知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-09-09Android 8.0實(shí)現(xiàn)發(fā)送通知
這篇文章主要為大家詳細(xì)介紹了Android 8.0實(shí)現(xiàn)發(fā)送通知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓圈倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn)
這篇文章主要介紹了Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Activity isFinishing()判斷Activity的狀態(tài)實(shí)例
下面小編就為大家分享一篇Activity isFinishing()判斷Activity的狀態(tài)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03