Android 簡單談談widget

來源:互聯網
上載者:User

日期widget

DatePickerDatePickerDialogDatePickerDialog是裝載DatePicker的一個簡單的容器,。分別有一個觸發方法OnDateChangedListener( ) 和OnDateSetListener( )。

在這個例子中,我們設定了兩個button和一個textView,當按鍵彈出DatePickDialog。

步驟1:一些有關時間的java函數

獲得目前時間的執行個體:Calendar calendar = Calendar.getInstance();

獲得目前時間:calendar.get(Calendar.YEAR),通過設定參數可獲得年,月,日,時,分,秒

設定時間:calendar.set(Calendar.YEAR,2011),可設定年,月,日,時,分,秒

用String給出當前的時間資訊,可以使用Java的SimpleDateFormat,如下處理:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //可以設定不同的類型。通過sdf.format(calendar.getTime()就可以獲得相關的info string,可供出來

步驟2:設定Android XML檔案並編寫有關的代碼略去)

步驟3:彈出日期Dialog,並設定Set的觸發回呼函數

java代碼:

 
  1. new DatePickerDialog(   
  2. /*參數1:context,在我的例子是內部類中調用,所有需指明this是那個this*/ Chapter9Test1.this,   
  3. /*參數2:設定Set日期的回呼函數*/ dateSet,   
  4. /*參數3,4,5:設定的年月日*/ calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show() ; 

最後一個show()表示將dialog顯示出來。Set的回呼函數,是OnDateSetListener(),如下:

java代碼

 
  1. DatePickerDialog.OnDateSetListener dateSet = new DatePickerDialog.OnDateSetListener() {  
  2. public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
  3. calendar.set(Calendar.YEAR, year);  
  4. calendar.set(Calendar.MONTH, monthOfYear);  
  5. calendar.set(Calendar.DATE, dayOfMonth);   
  6.   }  
  7. }; 

類比時鐘和數字時鐘

前面的例子,我們通常要設定某個日期或者時間,如果我們只是想向使用者顯示當前的時間,可以採用類比始終和數字時鐘。,下面是相關的Android XML檔案:

java代碼:

 
  1. <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
  2. <AnalogClock android:id="@+id/c91_analog" 
  3. android:layout_width="fill_parent" android:layout_height="wrap_content"   
  4. android:layout_centerHorizontal="true" 
  5. android:layout_alignParentTop="true" /> 
  6. <DigitalClock android:id="@+id/c91_digital" 
  7. android:layout_width="wrap_content" android:layout_height="wrap_content"   
  8. android:layout_centerHorizontal="true" 
  9. android:layout_below="@id/c91_analog" /> 
  10. </RelativeLayout>  

 

Android開發中插入新的Activity

在Android應用程式中使用Internet資料

殊途不同歸 Android三大製造商發展之路解析

Android平台開放性使其日益獲應用開發商青睞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.