android DatePicker控制項的使用

來源:互聯網
上載者:User

建立DatePickerDialog的步驟:

1、申明一個監聽器,使用匿名內部類:

DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.
          OnDateSetListener() {     
     @Override
     public void onDateSet(DatePicker view, int year, int monthOfYear,
       int dayOfMonth) {
      System.out.println(year + ":" + monthOfYear + ":" + dayOfMonth);
     }
  };

2、複寫Activity類的protected Dialog onCreateDialog(int id)方法

@Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DATE_PICKER_ID:
   return new DatePickerDialog(this, onDateSetListener, 2011, 8, 26);
  }
  return null;
 }

3、在需要顯示日期的時候調用showDialog(DATE_PICKER_ID)方法

 

例子:

main.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
 />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="顯示日期"
    />
</LinearLayout>

 

java代碼:

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerActivity extends Activity {
 
 private static final int DATE_PICKER_ID = 1;
 private Button button = null;
 private TextView tv = null;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  tv = (TextView) findViewById(R.id.textView);
  
  button = (Button) findViewById(R.id.button);
  button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DATE_PICKER_ID);//此方法用於顯示DatePickerDialog
   }
  });
 }

 //監聽器,監聽使用者點下DatePickerDialog的set按鈕時,所設定的年月日
 private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
   tv.setText(year + ":" + monthOfYear + ":" + dayOfMonth);
  }
 };

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DATE_PICKER_ID:
   return new DatePickerDialog(this, onDateSetListener, 2011, 8, 26);
  }
  return null;
 }
}

更詳細的請參看google api

相關文章

聯繫我們

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