世事洞明皆学问,人情练达即文章。这篇文章主要讲述Android DatePickerDialogTimePickerDialog相关的知识,希望能为你提供帮助。
文章图片
文章图片
文章图片
文章图片
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical" > 5 6< Button 7android:layout_width="match_parent" 8android:layout_height="wrap_content" 9android:onClick="datePickerDialog" 10android:text="DatePickerDialog" /> 11 12< Button 13android:layout_width="match_parent" 14android:layout_height="wrap_content" 15android:onClick="timePickerDialog" 16android:text="TimePickerDialog" /> 17 18 < /LinearLayout>
activity_main.xml
文章图片
文章图片
1 public class MainActivity extends Activity { 2 3@Override 4protected void onCreate(Bundle savedInstanceState) { 5super.onCreate(savedInstanceState); 6setContentView(R.layout.activity_main); 7} 8 9/** 10* DatePickerDialog和TimePickerDialog都继承于AlertDialog,基本方法和DataPicker和TimePicker组件一样, 11* 只需要调用Dialog的show()方法来显示,在构造方法里传入回调监听 12* @param v 13*/ 14int year = Calendar.getInstance().get(Calendar.YEAR); 15int month = Calendar.getInstance().get(Calendar.MONTH); 16int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 17public void datePickerDialog(View v){ 18DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() { 19@Override 20public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { 21Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); 22 23} 24}, year, month, dayOfMonth); 25dialog.show(); 26} 27 28int hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 29int minute = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); 30public void timePickerDialog(View v){ 31TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { 32 33@Override 34public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 35Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); 36} 37}, hourOfDay, minute, true); 38dialog.show(); 39} 40 }
MainActivity.java【Android DatePickerDialogTimePickerDialog】
推荐阅读
- Android Toast提示按两次返回键退出
- Android Toast自定义
- android笔记:DatePickerDialog日期设置对话框
- android-数据存储之外部file存储(sdcard)
- Android大字体下,通话界面SIM卡运营商名称显示不全
- Android ProgressDialog加载进度
- Android 面试题2
- Android Facebook和Twitter分享
- AndroidDialogAlertDialog