首页 > 代码库 > Android对话框(三)DatePickerDialog and TimePikerDialog
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" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示日期对话框" /> <Button android: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; @Override protected 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() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 用户选择的时候触发 //具体的月份这里传回的比实际小了1 monthOfYear = monthOfYear+1; String string = year+"年"+monthOfYear+"月"+dayOfMonth+"日"; Toast.makeText(MainActivity.this, string, 1).show(); } }, year, monthOfYear, dayOfMonth); datePickerDialog.show(); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //最后一个参数表是否为24小时形式 TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub String string = hourOfDay+"时"+minute+"分"; Toast.makeText(MainActivity.this, string, 1).show(); } }, hourOfDay, minute, true); timePickerDialog.show(); } }); } }
Android对话框(三)DatePickerDialog and TimePikerDialog
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。