时间组件DatePicker和TimePicker方面的总结

简介: MainActivity.java     package com.example.uidemo2;   import android.R.integer; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateS

MainActivity.java

 

 

package com.example.uidemo2;

 

import android.R.integer;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.DatePickerDialog.OnDateSetListener;

import android.app.TimePickerDialog;

import android.app.TimePickerDialog.OnTimeSetListener;

import android.content.DialogInterface;

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;

 

public class MainActivity extends Activity {

        

         //A view for selecting the time of day, in either 24 hour or AM/PM mode

         TimePicker timePicker;

         //This class is a widget for selecting a date.

         //The date can be selected by a year, month, and day

         DatePicker datePicker;

        

         Button button1;

         Button button2;

        

         TimePickerDialog timePickerDialog;

         DatePickerDialog datePickerDialog;

        

         //如果想对TimePicker做设置得是使用OnTimeSetListener

         OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {

                   @Override

                   public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                            timePicker.setCurrentHour(hourOfDay);

                            timePicker.setCurrentMinute(minute);

                   }

         };

         OnDateSetListener onDateSetListener = new OnDateSetListener() {

                  

                   @Override

                   public void onDateSet(DatePicker view, int year, int monthOfYear,

                                     int dayOfMonth) {

                            //datePicker设置年月日使用init方法

                            datePicker.init(year, monthOfYear, dayOfMonth, null);

                   }

         };

 

         private void fun2() {

                   /*

                    * android.app.DatePickerDialog.DatePickerDialog(Context context,

                    * OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

                    *

                    * Parameters:

                    * context The context the dialog is to run in.

                    * callBack How the parent is notified that the date is set.

                    * year The initial year of the dialog.

                    * monthOfYear The initial month of the dialog.

                    * dayOfMonth The initial day of the dialog.

                    * */

                   datePickerDialog = new DatePickerDialog(this, onDateSetListener, 2012, 11, 29);

                   datePickerDialog.setTitle("设置日期");

                   datePickerDialog.setMessage("设置闹钟日期");

                   datePickerDialog.setCancelable(false);

                   datePickerDialog.show();

         }

        

         public void fun1(){

                   /*

                    * android.app.TimePickerDialog.TimePickerDialog(Context context,

                    * OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

                    *

                    * Parameters:

                    * context Parent.

                    * callBack How parent is notified.

                    * hourOfDay The initial hour.

                    * minute The initial minute.

                    * is24HourView Whether this is a 24 hour view, or AM/PM.

                    */

                   timePickerDialog = new TimePickerDialog(this, onTimeSetListener, 0, 0, true);

                   timePickerDialog.setTitle("时间设置");

                   timePickerDialog.setMessage("设置闹钟时间");

                  

                   /* Sets whether this dialog is cancelable with the BACK key

                    * 设置这个dialog是否能够被back键取消

                    * */

                   timePickerDialog.setCancelable(false);

                  

                   timePickerDialog.setButton(DialogInterface.BUTTON1, "确定",timePickerDialog);

                   timePickerDialog.setButton(DialogInterface.BUTTON2, "退出",(android.content.DialogInterface.OnClickListener)null);

                   timePickerDialog.show();

         }

        

        

         OnClickListener onClickListener = new OnClickListener() {

                  

                   @Override

                   public void onClick(View v) {

                            switch (v.getId()) {

                            case R.id.button1:

                                     fun1();

                                     break;

                            case R.id.button2:

                                     fun2();

                                     break;

                            default:

                                     break;

                            }       

                   }

         };

        

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        timePicker = (TimePicker) this.findViewById(R.id.timePicker1);

        datePicker = (android.widget.DatePicker) this.findViewById(R.id.datePicker1);

        

        button1 = (Button) this.findViewById(R.id.button1);

        button2 = (Button) this.findViewById(R.id.button2);

       

        button1.setOnClickListener(onClickListener);

        button2.setOnClickListener(onClickListener);

    }

 

}

 

 

activity_main.xml

<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"

   

     >

 

    <TimePicker

        android:id="@+id/timePicker1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

 

    <DatePicker

        android:id="@+id/datePicker1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="timepickerDialog" />

 

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="datepickerDialog" />

 

</LinearLayout>

 

 

 

 

目录
相关文章
|
7月前
|
JavaScript
vue element plus TimePicker 时间选择器
vue element plus TimePicker 时间选择器
90 0
|
自然语言处理 前端开发 JavaScript
最好用的 8 款 React Datepicker 时间日期选择器测评推荐
React 时间日期选择器(date-timepicker)组件,与表单、富文本、表格、拖拽等组件一样,是大家用 React 搭建项目时使用最频繁的组件之一。React DateTimePicker 除了基础选择日期时间外,还有非常多样的功能配合不同场景使用,比如 12/24小时,禁止选择某些日期,高亮某些日期,夜间模式,多语言,酒店订单的特别场景等。本文记录了我自己使用多年最好用的 8 款 React DateTimePicker 组件,每一款都经过我实际测试,推荐给大家。
1868 0
最好用的 8 款 React Datepicker 时间日期选择器测评推荐
|
15天前
|
资源调度 前端开发 开发者
React 日期选择器 Date Picker
本文介绍了如何在 React 应用中使用 `react-datepicker` 日期选择器,涵盖基础安装、基本用法、自定义样式、限制可选日期、多日期选择等内容,并探讨了常见问题及解决方案,帮助开发者在实际项目中灵活应用日期选择器。
27 5
|
7月前
|
JavaScript UED
【vue】iview组件 DatePicker 日期选择器如何显示默认当前日期
【vue】iview组件 DatePicker 日期选择器如何显示默认当前日期
443 1
|
JavaScript 前端开发 程序员
DatePicker 日期选择器,获取系统默认当前年月日
DatePicker 日期选择器,获取系统默认当前年月日
503 0
|
前端开发 JavaScript
react获取Datepicker组件日期
react获取Datepicker组件日期
|
Java Android开发
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
927 0
【安卓开发】Android中日期选择器DatePicker和TimePicker的使用
BootStrop datepicker 选择年月
BootStrop datepicker 选择年月
102 0