您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android中使用DatePickerDialog和TimePickerDialog實(shí)現(xiàn)時(shí)間日期對(duì)話框,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
用法:
一、創(chuàng)建兩個(gè) DatePickerDialog、TimePickerDialog 實(shí)例調(diào)用 show() 方法即可將他們顯示出來(lái)
二、為 DatePickerDialog、TimePickerDialog 實(shí)例分別綁定監(jiān)聽(tīng)器,通過(guò)監(jiān)聽(tīng)獲得用戶設(shè)置
效果:
DatePickerDialog
TimePickerDialog
下面是具體的實(shí)現(xiàn)方法:
public class MainActivity extends Activity { private Button buttonDate; private Button buttonTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonDate = (Button) findViewById(R.id.dataBn); buttonTime = (Button) findViewById(R.id.timeBn); iniClick();//Binding the listeners for you program } public void iniClick(){ //set listener for your Date button buttonDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); //create a datePickerDialog and then shoe it on your screen new DatePickerDialog(MainActivity.this,//binding the listener for your DatePickerDialog new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this,"Year:" + year + " Month:" + month + " Day:" + dayOfMonth,Toast.LENGTH_SHORT).show(); } } , calendar.get(Calendar.YEAR) , calendar.get(Calendar.MONTH) , calendar.get(Calendar.DAY_OF_MONTH)).show(); } }); //set listener for your Time button buttonTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); //create a datePickerDialog and then shoe it on your screen new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this,"Hour:" + hourOfDay + " Minute:" + minute ,Toast.LENGTH_SHORT).show(); } } , calendar.get(Calendar.HOUR_OF_DAY) , calendar.get(Calendar.MINUTE) , true).show(); } }); } }
這里是布局文件:
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/idtatabHost" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/dataBn" android:text="點(diǎn)我一下 挑日期" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/timeBn" android:text="點(diǎn)我一下 挑時(shí)間 。。。" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout>
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
上述內(nèi)容就是怎么在Android中使用DatePickerDialog和TimePickerDialog實(shí)現(xiàn)時(shí)間日期對(duì)話框,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。