TimePickerDialog(时间选择对话框)
创建TimePickerDialog时间选择对话框:
1.创建一个类继承DialogFragement
2.重写onCreateDialog()方法,返回一个TimePickerDialog对象
3.实现TimePickerDialog的OnTimeSetListener接口来接收一个回调,当用户设置时间
DatePickerDialog(日期选择对话框)
创建DatePickerDialog日期选择对话框:
1.创建一个类继承DialogFragment
2.重写onCreateDialog()方法,返回一个TimePickerDialog对象
3.实现DatePickerDialog的OnDateSetListener接口来接收一个回调,当用户设置日期
DatePickerDialog和TimePickerDialog是Android提供的可以弹出一个日期、时间选择的对话框,我们可以在程序中通过实例化DatePickerDialog和TimePickerDialog来得到一个日期、时间对话框。这两个类都是AlertDialog的子类
我提供了一个百度的关于Picker的官方说明,可能对理解这篇文章有帮助:http://wenku.baidu.com/link?url=U77TPsl99lpf8QrGD2h6XKA6xJqzk-JseCnJhC9VmgYoWckhCZ8-3F3TzOkOZGLverjwt6JitlhW-wWMODAOQLtEpVxryaZxNlCQstOjLCK
源码:
html
<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"> <Button android:id="@+id/button1_settime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Time" android:onClick="setTimeClick"/> <TextView android:id="@+id/tv_settime" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/button1_setdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Date" android:onClick="setDateClick"/> <TextView android:id="@+id/tv_setdate" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
java
<span style="font-size:12px;">import java.util.Calendar; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.TimePickerDialog; import android.text.format.DateFormat; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; @SuppressLint({ "NewApi", "ValidFragment" }) public class MainActivity extends Activity { private Button btnSetTime; private TextView tv_setTime; private TextView tv_setDate; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化对象,通过findViewById和相应的xml布局组件绑定 btnSetTime = (Button) findViewById(R.id.button1_settime); tv_setTime = (TextView) findViewById(R.id.tv_settime); tv_setDate = (TextView) findViewById(R.id.tv_setdate); } //设置日期事件处理方法 public void setDateClick(View v){ SetDateDialog sdt = new SetDateDialog(); sdt.show(getFragmentManager(), "datePicker"); } //设置时间事件处理方法 public void setTimeClick(View v){ SetTimeDialog std = new SetTimeDialog(); std.show(getFragmentManager(), "timePicker"); } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } //创建日期选择对话框 class SetDateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{ public Dialog onCreateDialog(Bundle savedInstanceState) { //Calendar 是一个抽象类,是通过getInstance()来获得实例,设置成系统默认时间 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); DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day); return dpd; } public void onDateSet(DatePicker view, int year, int month, int day) { tv_setDate.setText(year+"年"+(month+1)+"月"+day+"日"); } } //创建时间选择对话框 @SuppressLint("NewApi") class SetTimeDialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener{ @SuppressLint("NewApi") public Dialog onCreateDialog(Bundle savedInstanceState) { //获取小时,分钟 final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); TimePickerDialog tpd = new TimePickerDialog(getActivity(),this,hour, minute, DateFormat.is24HourFormat(getActivity())); return tpd; } public void onTimeSet(TimePicker view, int hourOfDay, int minute){ tv_setTime.setText(hourOfDay+":"+minute); } } }</span>
效果展示:
时间: 2024-10-22 09:48:52