本文主要是介绍Android对话框(三)DatePickerDialog and TimePikerDialog,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有时间再写个日期时间合在一起的自定义时间选择对话框
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.dateandtimedialog.MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="显示日期对话框" /><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/button1"android:text="显示时间对话框" /></RelativeLayout>
package com.example.dateandtimedialog;import java.util.Calendar;import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;public class MainActivity extends Activity {private Button button1; private int year;private int monthOfYear;private int dayOfMonth;private Button button2; private int hourOfDay;private int minute;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);//对年月日的初始化Calendar calendar = Calendar.getInstance();year = calendar.get(Calendar.YEAR);monthOfYear = calendar.get(Calendar.MONTH);dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);minute = calendar.get(Calendar.MINUTE);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubDatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {// 用户选择的时候触发//具体的月份这里传回的比实际小了1monthOfYear = monthOfYear+1;String string = year+"年"+monthOfYear+"月"+dayOfMonth+"日";Toast.makeText(MainActivity.this, string, 1).show();}}, year, monthOfYear, dayOfMonth);datePickerDialog.show();}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//最后一个参数表是否为24小时形式TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stubString string = hourOfDay+"时"+minute+"分";Toast.makeText(MainActivity.this, string, 1).show();}}, hourOfDay, minute, true);timePickerDialog.show();}});}
}
源代码下载
这篇关于Android对话框(三)DatePickerDialog and TimePikerDialog的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!