Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 時間選擇器與日期選擇器的實現代碼分享

編輯:關於 Android 程式設計

本文為大家分享兩款常用的選擇器:一款用於選擇時間,一款用於選擇日期,具體內容如下。

一、時間選擇器

1. 布局檔:在activity中放入一個用於顯示結果的 TextView 與一個觸發選擇的 Button。

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.rj141.sb.kongjian.DateActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="幾點吃飯:" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/tv" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="時間" android:id="@+id/btndate" /> </LinearLayout> 

2. Java 檔:點擊按鈕後彈出 TimePickerDialog,在 onTimeSet 回呼中取得時與分並顯示。

 public class DateActivity extends ActionBarActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); btn=(Button)this.findViewById(R.id.btndate); tv= (TextView) this.findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { tv.setText(String.format("%d:%d",hourOfDay,minute)); } //0,0指的是時間,true表示是否為24小時,true為24小時制 },0,0,true).show(); } }); } } 

效果圖:

二、日期選擇器

1. 布局檔 activity.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.rj141.sb.kongjian.DateActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/tv" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日歷" android:id="@+id/btndate" /> </LinearLayout> 

2. DateActivity.java:點擊按鈕後彈出 DatePickerDialog,透過 OnDateSetListener 回呼取得年、月、日。需要注意的是月份參數從 0 開始,顯示時需加 1。

 public class DateActivity extends ActionBarActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); btn=(Button)this.findViewById(R.id.btndate); tv= (TextView) this.findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { tv.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth)); } },2000,1,2).show(); } }); } } DatePickerDialog日歷選擇器的對話框,監聽為OnDateSetListener(){..} 

效果圖:

以上就是兩款 Android 時間選擇器與日期選擇器的實現代碼,希望對大家學習 Android 軟體編程有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved