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

Android 常用選擇器組件 CalendarView、DatePicker、TimePicker 與 NumberPicker 使用大全

編輯:關於 Android 程式設計

本文一起複習幾個常用的基礎 UI 組件。Android 對這些組件封裝得非常完善,基本上套上屬性就能直接用,極大減輕了開發者的負擔;當然如果想深入研究,這其中仍有不少值得細細分析的空間。先看幾個簡單示例。

先上效果圖:

CalendarView 效果:

ChooseView 效果:

NumberPicker 效果

一、CalendarView

main.xml 中只需加上幾個簡單屬性即可:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇您的生日:"/> <!-- 設置以星期二作為每周第一天 設置該組件總共顯示4個星期 並對該組件的日期時間進行了定制 --> <CalendarView android:layout_width="match_parent" android:layout_height="match_parent" android:firstDayOfWeek="3" android:shownWeekCount="4" android:selectedWeekBackgroundColor="#aff" android:focusedMonthDateColor="#f00" android:weekSeparatorLineColor="#ff0" android:unfocusedMonthDateColor="#f9f" android:id="@+id/calendarView" /> </LinearLayout> 

Activity 代碼:

 public class MainActivity extends Activity { CalendarView cv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cv = (CalendarView)findViewById(R.id.calendarView); // 為CalendarView組件的日期改變事件添加事件監聽器 cv.setOnDateChangeListener(new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { // 使用Toast顯示用戶選擇的日期 Toast.makeText(MainActivity.this, "你生日是" + year + "年" + month + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show(); } }); } } 

二、DatePicker 與 TimePicker

結合 Calendar 使用,可以讓用戶方便地選擇日期與時間。

main.xml 代碼:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇購買本書的具體時間"/> <!-- 定義一個DatePicker組件 --> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:startYear="2000" android:endYear="2016" android:calendarViewShown="true" android:spinnersShown="true"/> <!-- 定義一個TimePicker組件 --> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center_horizontal"/> <!-- 顯示用戶輸入日期、時間的控件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> </LinearLayout> 

Activity 代碼:

 public class MainActivity extends Activity { // 定義5個記錄當前時間的變量 private int year; private int month; private int day; private int hour; private int minute; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker); TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker); // 獲取當前的年、月、日、小時、分鐘 Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR); minute = c.get(Calendar.MINUTE); // 初始化DatePicker組件,初始化時指定監聽器 datePicker.init(year, month, day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker arg0, int year , int month, int day) { MainActivity.this.year = year; MainActivity.this.month = month; MainActivity.this.day = day; // 顯示當前日期、時間 showDate(year, month, day, hour, minute); } }); timePicker.setEnabled(true); // 為TimePicker指定監聽器 timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view , int hourOfDay, int minute) { MainActivity.this.hour = hourOfDay; MainActivity.this.minute = minute; // 顯示當前日期、時間 showDate(year, month, day, hour, minute); } }); } // 定義在EditText中顯示當前日期、時間的方法 private void showDate(int year, int month , int day, int hour, int minute) { EditText show = (EditText) findViewById(R.id.show); show.setText("您的購買日期為:" + year + "年" + (month + 1) + "月" + day + "日 " + hour + "時" + minute + "分"); } } 

三、NumberPicker

NumberPicker 主要用在需要用戶選擇數字的場景,例如價格區間。

main.xml 代碼:

 <TableLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="選擇低價:" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np1" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="選擇高價:" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np2" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> </TableLayout> 

Activity 代碼:

 public class MainActivity extends Activity { NumberPicker np1, np2; // 定義最低價格、最高價格的初始值 int minPrice = 25, maxPrice = 75; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); np1 = (NumberPicker) findViewById(R.id.np1); // 設置np1的最小值和最大值 np1.setMinValue(10); np1.setMaxValue(50); // 設置np1的當前值 np1.setValue(minPrice); np1.setOnValueChangedListener(new OnValueChangeListener() { // 當NumberPicker的值發生改變時,將會激發該方法 @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { minPrice = newVal; showSelectedPrice(); } }); np2 = (NumberPicker) findViewById(R.id.np2); // 設置np2的最小值和最大值 np2.setMinValue(60); np2.setMaxValue(100); // 設置np2的當前值 np2.setValue(maxPrice); np2.setOnValueChangedListener(new OnValueChangeListener() { // 當NumberPicker的值發生改變時,將會激發該方法 @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { maxPrice = newVal; showSelectedPrice(); } }); } private void showSelectedPrice() { Toast.makeText(this, "您選擇最低價格為:" + minPrice + ",最高價格為:" + maxPrice, Toast.LENGTH_SHORT) .show(); } } 

以上就是對這幾個常用選擇器組件的梳理,希望能幫助大家快速上手,也感謝大家的閱讀與支持。

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