Blogtrottr
海芋小站
提供實用軟體教學,介紹有趣網站及資訊,豐富電腦人生 
Find Unique T-shirts

Explore the most popular t-shirts on Cafepress. Discover great designs celebrating anything you can think of.
From our sponsors
[Android 開發] DatePickerDialog 日期選擇器的使用
Jun 16th 2014, 11:18, by 張海芋

在 Android 的開發上,讓使用者選擇日期是一件很常做的事期, Android 內其實內建日期選擇工具 DatePickerDialog ,只要透過這個對話框,就可以讓使用者快速設定日期,如果你是 Android 的開發者,那麼這個工具一定不可以錯過。

假設,我們有一個 Android 的 EditText ,當我們按下這個 EditText 時,就希望跳出日期選擇工具,讓使用者設定日期,該怎麼做呢?首先,我們當然要刻 EditText 啦,如下的範例。
 

1
2
3
4
5
6
<EditText
android:id="@+id/edit_birthday"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="date"
android:singleLine="true" />

 

接下來,就得在程式碼中加入 EditText 的監聽事件,範例如下:

1
2
3
private EditText etBirthday = null;
etBirthday = (EditText) findViewById(R.id.edit_birthday);
etBirthday.setOnClickListener(this);

在 EditText 點下去之後,要出現 DatePickerDialog 讓使用者選擇日期,所以我們先調用 DatePickerDialog 的對話框

1
2
3
4
DatePickerDialog dialog = new DatePickerDialog(context, datepicker, 
                                                m_Calendar.get(Calendar.YEAR), 
                                                m_Calendar.get(Calendar.MONTH),
                                                m_Calendar.get(Calendar.DAY_OF_MONTH));

其中, datepicker 為 DatePickerDialog 的 Callback,因為我們是要讓使用者設定日期時,再於 EditText 來顯示日期,所以只要實做 OnDateSetListener 這個 Listener 就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
DatePickerDialog.OnDateSetListener datepicker = new DatePickerDialog.OnDateSetListener() 
{
 
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
        {
            m_Calendar.set(Calendar.YEAR, year);
            m_Calendar.set(Calendar.MONTH, monthOfYear);
            m_Calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            String myFormat = "yyyy/MM/dd";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.TAIWAN);
            etBirthday.setText(sdf.format(m_Calendar.getTime()));
        }
};

整體 JAVA 程式碼如可以拚湊如下,至於內容依個人實做需求和 XML 檔設計來做調整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class SubmitBirthdayActivity extends Activity implements OnClickListener
{
 
        private Calendar   m_Calendar                      = Calendar.getInstance();
        private EditText        etBirthday                      = null;
 
 
        protected void onResume()
        {
                etBirthday = (EditText) findViewById(R.id.edit_birthday);
                etBirthday.setOnClickListener(this);        
        }
 
        DatePickerDialog.OnDateSetListener datepicker = new DatePickerDialog.OnDateSetListener() 
        {
 
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
                {
                        m_Calendar.set(Calendar.YEAR, year);
                    m_Calendar.set(Calendar.MONTH, monthOfYear);
                    m_Calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    String myFormat = "yyyy/MM/dd"; //In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.TAIWAN);
                    etBirthday.setText(sdf.format(m_Calendar.getTime()));
                }
        };
 
        public void onClick(View v)
        {
                if (v.getId() == etBirthday.getId())
                {
                    DatePickerDialog dialog = new DatePickerDialog(SubmitBirthdayActivity.this, 
                                                                datepicker, 
                                                                m_Calendar.get(Calendar.YEAR), 
                                                                m_Calendar.get(Calendar.MONTH),
                                                                m_Calendar.get(Calendar.DAY_OF_MONTH));
                    dialog.show();                  
                }     
        }     
}
留言回應 或者 訂閱 RSS feed 以獲得更多的文章更新資訊。若有商業上的需求,也歡迎透過 合作提案 與我聯絡。本站文章非經授權請勿「全文轉貼」,引用時請註明來源,謝謝。

This entry passed through the Full-Text RSS service — if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

You are receiving this email because you subscribed to this feed at blogtrottr.com.

If you no longer wish to receive these emails, you can unsubscribe from this feed, or manage all your subscriptions
arrow
arrow
    全站熱搜

    xals2q 發表在 痞客邦 留言(0) 人氣()