一.日期对话框
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.wang.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 13 14 <Button 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="日期对话框" 18 android:onClick="bt6_OnClick" 19 /> 20 21 22 23 </LinearLayout>
1 package com.example.wang.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.opengl.ETC1Util; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.widget.DatePicker; 13 import android.widget.EditText; 14 import android.widget.ImageView; 15 import android.widget.TimePicker; 16 import android.widget.Toast; 17 18 import java.util.Calendar; 19 20 public class TestActivity5 extends AppCompatActivity { 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_test5); 26 27 } 28 29 //当前日期 日历 30 31 //单例模式 32 Calendar c=Calendar.getInstance(); 33 34 35 //日期对话框 36 public void bt6_OnClick(View v) 37 { 38 39 DatePickerDialog dp=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 40 @Override 41 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 42 43 Toast.makeText(TestActivity5.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); 44 45 } 46 }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); 47 48 dp.setCancelable(false); 49 50 dp.show(); 51 } 52 53 }
二.时间对话框
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.wang.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="时间对话框" 17 android:onClick="bt7_OnClick" 18 /> 19 20 21 </LinearLayout>
1 package com.example.wang.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.opengl.ETC1Util; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.widget.DatePicker; 13 import android.widget.EditText; 14 import android.widget.ImageView; 15 import android.widget.TimePicker; 16 import android.widget.Toast; 17 18 import java.util.Calendar; 19 20 public class TestActivity5 extends AppCompatActivity { 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_test5); 26 27 } 28 29 //当前时间 30 31 //单例模式 32 Calendar c=Calendar.getInstance(); 33 34 35 36 //时间 37 public void bt7_OnClick(View v) 38 { 39 TimePickerDialog tp=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 40 @Override 41 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 42 43 Toast.makeText(TestActivity5.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); 44 45 } 46 }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true); 47 48 tp.setCanceledOnTouchOutside(false); 49 50 tp.show(); 51 } 52 }