参考网站:http://wear.techbrood.com/guide/topics/ui/controls/pickers.html
1、首先定义一个类DatePickerFragment继承DialogFragment,并实现接口DatePickerDialog.OnDateSetListener;
/**
* Created by czz on 2016/4/1.
*/
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),this,year,month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(getActivity(),year+"/" + monthOfYear + "/" + dayOfMonth,Toast.LENGTH_SHORT).show();
}
}
2、布局一个按钮或其他控件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间选择器"
android:id="@+id/id_selectTime"
android:layout_below="@+id/id_togglebtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp" />
3、声明 DialogFragment对象,调用show()方法
selectTime = (Button) findViewById(R.id.id_selectTime);
selectTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment TimeDF = new DatePickerFragment();
TimeDF.show(getFragmentManager(),"datePicker");
}
});