安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog

基于API17的组件

一.TimePicker

安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog_第1张图片

<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timePicker"

android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="saveClick"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
 
private TimePicker timePicker;
 
timePicker= (TimePicker) findViewById(R.id.timePicker);
 
public void saveClick(View v)
{
int hour= timePicker.getCurrentHour();
int minute=timePicker.getCurrentMinute();
Toast.makeText(this,hour+":"+minute,Toast.LENGTH_SHORT).show();
}
 

二.TimePickerDialog

安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog_第2张图片

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="not time"
android:id="@+id/textView_time" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置时间 "
android:onClick="setTimeClick"
android:id="@+id/button_setTime"
android:layout_below="@+id/textView_time"
android:layout_alignParentStart="true" />
 
(1)创建一个类TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
public  class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
int hour;
int minute;
private MainActivity mainActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//通过getActivity方法获取该Fragment所依赖的Activity对象
mainActivity=(MainActivity)getActivity();

}

@Override
/*创建对话框的事件方法,该方法会在MainActivity的按钮事件中调用show方法时,会检查是否已经存在dialog
* 当不存在时会触发该事件,否则直接显示
* */
//显示dialog时,自动调用onCreateDialog方法
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;
mainActivity.setTimeValue(hour,minute);
}
}
(2)在MainActivity中调用
public class MainActivity extends Activity {

private TextView textView_time;




protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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)
{
DialogFragment timePickerFragment=new TimePickerFragment();
timePickerFragment.show(getFragmentManager(),"timePicker");
}
}

三.DatePicker

安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog_第3张图片

<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_below="@+id/timePicker"
android:layout_alignParentStart="true"
android:calendarViewShown="false" //是否显示日历
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:onClick="saveClick1"
android:id="@+id/button2"
android:layout_below="@+id/datePicker"
android:layout_alignParentStart="true" />
private DatePicker datePicker;
 
datePicker=(DatePicker)findViewById(R.id.datePicker);
 
  public void saveClick1(View v)
{
int year=datePicker.getYear();
int month=datePicker.getMonth()+1;
int day=datePicker.getDayOfMonth();

Toast.makeText(this,year+":"+month+":"+day,Toast.LENGTH_SHORT).show();
}

四.DatePickerDialog

安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog_第4张图片

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="not date"
android:id="@+id/textView_date" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置日期 "
android:onClick="setDateClick"
android:id="@+id/button_setDate"
android:layout_below="@+id/textView_date"
android:layout_alignParentStart="true" />
 
(1)创建一个类DatePickerFragment继承 DialogFragment
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

private MainActivity2 mainActivity2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity2= (MainActivity2) getActivity();
}

@Override
//用于创建日期对话框的事件
public Dialog onCreateDialog(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;
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

mainActivity2.setDateValue(year,monthOfYear,dayOfMonth);

}
}
 
(2)在MainActivity中创建对象调用
public class MainActivity2 extends Activity {
private TextView textView_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView_date= (TextView) findViewById(R.id.textView_date);
}

/*设置日期的按钮事件 */
public void setDateClick(View v)
{
DialogFragment dateDialogFragment=new DatePickerFragment();
dateDialogFragment.show(getFragmentManager(),"datePicker");
}
public void setDateValue(int year,int month,int day)
{
textView_date.setText(year+"年"+(month+1)+"月"+day+"日");

}
}
 
 

你可能感兴趣的:(安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog)