Android學習筆記之DatePicker

來源:互聯網
上載者:User

<1>簡介

一個選擇年月日的日曆布局視圖

<2>類繼承

java.lang.Object

      android.view.View

        android.view.ViewGroup

          android.widget.FrameLayout

                               android.widget.DatePicker

<3>方法

public int getDayOfMonth ()

         擷取選擇的天數

         public int getMonth ()

         擷取選擇的月份。(注意:返回數值為0..11,需要自己+1來顯示)

         public int getYear ()

         擷取選擇的年份

         public void init (int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)

         初始化狀態。(譯者註:初始化年月日)

                   參數

                            year         初始年(譯者註:注意使用new Date()初始化年時,需要+1900,如下:date.getYear()
+ 1900

                            monthOfYear 初始月。

                            dayOfMonth  初始日。

                            onDateChangedListener        
日期改變時通知使用者的事件監聽,可以為空白(null)。

         public void setEnabled (boolean enabled)

         設定視圖的啟用狀態。該啟用狀態隨子類的不同而有不同的解釋。

                   參數

                            enabled  設定為true表示啟動視圖,反之禁用。

         public void updateDate (int year, int monthOfYear, int dayOfMonth)

         更新日期

範例:

package android.test;import android.app.Activity;import android.os.Bundle;import android.widget.DatePicker;import android.widget.Toast;public class TestActivity extends Activity {    /** Called when the activity is first created. */private DatePicker datepicker = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                datepicker = (DatePicker)findViewById(R.id.datepicker);        String date = "年:"+datepicker.getYear()+"月:"+datepicker.getMonth()+"日:"+datepicker.getDayOfMonth();        Toast.makeText(TestActivity.this, date, Toast.LENGTH_SHORT).show();            }}

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="日期選取器(DatePicker)" />      <DatePicker         android:id="@+id/datepicker"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        /></LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.