Android为提供了一个随时可用的对话框,方便用户选取时间或者日期。今天我们就简单的学习一下Picker的使用。我们的爱,比死亡还要理所当然。
Picker的使用
项目结构如下:
一、创建一个Time Picker
创建一个TimePickerFragment
class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + ":" + minute; timeView.setText(time); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); return new TimePickerDialog(getActivity(), this, hours, minutes, DateFormat.is24HourFormat(getActivity())); } }
在mainActivity中使用:
// 显示时间的picker public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getFragmentManager(), "timePicker"); }
二、创建一个Date Picker
创建一个DatePickerFragment
class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { String date = year + "-" + month + "-" + day; dateView.setText(date); } }
在mainActivity中使用:
// 显示日期的picker public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); }
上述的运行效果如下:
三、测试的完整代码
MainActivity.java
package com.huhx.linux.pickertest; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.format.DateFormat; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private TextView timeView; private TextView dateView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timeView = (TextView) findViewById(R.id.time); dateView = (TextView) findViewById(R.id.date); } // 显示时间的picker public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getFragmentManager(), "timePicker"); } // 显示日期的picker public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); } class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { String date = year + "-" + month + "-" + day; dateView.setText(date); } } class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + ":" + minute; timeView.setText(time); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); return new TimePickerDialog(getActivity(), this, hours, minutes, DateFormat.is24HourFormat(getActivity())); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.huhx.linux.pickertest.MainActivity"> <TextView android:id="@+id/time" android:text="time" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="date" android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showTimePickerDialog" android:text="picker time" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showDatePickerDialog" android:text="picker date" /> </LinearLayout>