1、TimePickerDialog
1)创建一个类继承DialogFragment
2)重写onCreateDialog()方法,返回一个TimePickerDialog对象
3)实现TimePickerDialog的OnTimeSetListener接口来接收一个回调,当用户设置时间
//TimerPickerFragment.java
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TimePicker;
import java.util.Calendar;
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
int hour;
int minute;
private MainActivity_time mainActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//通过getActivity()方法获取Fragment所依赖的Activity对象
mainActivity = (MainActivity_time) getActivity();
}
//创建对话框的事件方法,该方法会在MainActivity的按钮事件中调用show方法,会检查是否已存在Dialog
//当不存在会触发该事件,否则直接显示
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//获取当前系统时间
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
//创建时间对话框,参数(上下文,设置时间事件,小时,分钟,是否为24小时)
TimePickerDialog dialog = new TimePickerDialog(getActivity(),this,hour,minute,true);
return dialog;
}
//时间对话框中的“完成”按钮的单击事件
@Override
public void onTimeSet(TimePicker view,int hourOfDay,int minute){
this.hour = hourOfDay;
this.minute = minute;
//此处把用户设置的时间值设置到界面的TextView组件上
mainActivity.setTimeValue(hour,minute);
}
}
//MainActivity_time.java
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity_time extends AppCompatActivity {
private TextView textView_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
textView_time = (TextView) findViewById(R.id.textView_time);
}
public void setTimeValue(int hour,int minute){
textView_time.setText(hour+":"+minute);
}
//按钮单击事件
public void setTimeClick(View view){
TimePickerFragment timePickerFragment = new TimePickerFragment();
timePickerFragment.show(getFragmentManager(),"timePicker");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_time"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="25dp"
android:layout_marginTop="28dp"
android:text="not time" />
<Button
android:id="@+id/button_setTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="22dp"
android:layout_marginTop="92dp"
android:text="设置时间"
android:onClick="setTimeClick"/>
</RelativeLayout>
2、DatePickerDialog
//MainActivity_Date
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Date;
public class MainActivity_Date extends AppCompatActivity {
private TextView textView_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__date);
textView_date = findViewById(R.id.textView_date);
}
public void setDateValue(int year,int month,int day){
textView_date.setText(year+"年"+(month+1)+"月"+day+"日");
}
/***
* 设置日期的按钮事件
* @param v
*/
public void setDateClick(View v){
DialogFragment dateDialogFragment = new DatePickerFragment();
dateDialogFragment.show(getSupportFragmentManager(),"datePicker");
}
}
//DatePickerFragment.java
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private MainActivity_Date mainActivity_date;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity_date = (MainActivity_Date)getActivity();
}
//用于创建日期对话框
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,year,month,day);
return dialog;
}
/***
* 设置日期的按钮事件
* @param view
* @param year
* @param month
* @param dayOfMonth
*/
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mainActivity_date.setDateValue(year,month,dayOfMonth);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_Date">
<TextView
android:id="@+id/textView_date"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="not date"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="95dp" />
<Button
android:id="@+id/button_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="setDateClick"
android:text="设置日期"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="309dp" />
</RelativeLayout>
3、TimePicker和DatePicker
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity_TmDt extends AppCompatActivity {
private TimePicker timePicker;
private DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__tm_dt);
timePicker = (TimePicker) findViewById(R.id.timePicker);
datePicker = (DatePicker) findViewById(R.id.datePicker);
}
public void timeClick(View v){
int hour = timePicker.getHour();
int minute = timePicker.getMinute();
Toast.makeText(this,hour+":"+minute,Toast.LENGTH_SHORT).show();
}
public void dateClick(View view){
int year = datePicker.getYear();
int month = datePicker.getMonth();
int day = datePicker.getDayOfMonth();
Toast.makeText(this,year+"年"+month+"月"+day+"日",Toast.LENGTH_SHORT).show();
}
}