您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么用Android實(shí)現(xiàn)日期時(shí)間選擇對(duì)話框”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用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(); } }); } }
到此,相信大家對(duì)“怎么用Android實(shí)現(xiàn)日期時(shí)間選擇對(duì)話框”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(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)容。