时间选择(DatePicker和TimePicker)使用

2020-04-24 08:36:17 浏览数 (1)

image.png

目录

DatePicker

日期选择器,据有spinner和calendar两种模式.

DatePicker具有以下的属性值:

属性

含义

android:calendarTextColor

日历列表的文本的颜色

android:calendarViewShown

是否显示日历视图

android:datePickerMode

组件外观,可选值:spinner,calendar

android:dayOfWeekBackground

顶部星期几的背景颜色

android:dayOfWeekTextAppearance

顶部星期几的文字颜色

android:endYear

去年(内容)

android:firstDayOfWeek

设置日历列表以星期几开头

android:headerBackground

整个头部的背景颜色

android:headerDayOfMonthTextAppearance

头部日期字体的颜色

android:headerMonthTextAppearance

头部月份的字体颜色

android:headerYearTextAppearance

头部年的字体颜色

android:maxDate

最大日期显示在这个日历视图mm / dd / yyyy格式

android:minDate

最小日期显示在这个日历视图mm / dd / yyyy格式

android:spinnersShown

是否显示spinner

android:startYear:

设置第一年(内容),比如19940年

android:yearListItemTextAppearance

列表的文本出现在列表中。

android:yearListSelectorColor

年列表选择的颜色

使用方式:

代码语言:javascript复制
    <DatePicker
        android:id="@ id/datePickerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="true"
        android:datePickerMode="spinner"
        android:layout_gravity="center_horizontal" />

spinner形式:

image.png

calendar形式:

image.png

TimePicker

时间选择器,据有spinner和clock两种模式. 使用方式:

代码语言:javascript复制
    <TimePicker
        android:id="@ id/timepicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ffffff"
        android:timePickerMode="spinner"
        />

spinner形式:

image.png

clock形式:

image.png

实战

DatePicker

activity_main.xml文件:

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <DatePicker
        android:id="@ id/datePickerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="true"
        android:datePickerMode="calendar"
        android:layout_gravity="center_horizontal" />

</android.support.constraint.ConstraintLayout>

代码:

代码语言:javascript复制
package com.example.user.datapicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    DatePicker datePicker;

    int year,mouth,day,hour,min;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datePicker = (DatePicker)findViewById(R.id.datePickerView);

        // get time
        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        mouth =  cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR);
        min = cal.get(Calendar.MINUTE);

        datePicker.init(year, mouth, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                MainActivity.this.year = year;
                MainActivity.this.mouth = monthOfYear;
                MainActivity.this.day = dayOfMonth;

                //显示用户选择的日期
                Toast.makeText(MainActivity.this,year   "年"   monthOfYear   "月"   dayOfMonth   "日",Toast.LENGTH_LONG).show();
            }
        });

    }
}

执行效果:

image.png

TimePicker

activity_main.xml文件:

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TimePicker
        android:id="@ id/timepicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ffffff"
        android:timePickerMode="spinner"
        />

</android.support.constraint.ConstraintLayout>

代码:

代码语言:javascript复制
package com.example.user.datapicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    TimePicker timePicker;

    int year,mouth,day,hour,min;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timePicker = (TimePicker)findViewById(R.id.timepicker);

        // get time
        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        mouth =  cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR);
        min = cal.get(Calendar.MINUTE);

        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                //显示用户选择的日期
                Toast.makeText(MainActivity.this,hourOfDay   "时"   minute   "分",Toast.LENGTH_LONG).show();
            }
        });

    }
}

执行效果:

image.png

参考

Android中日期选择控件-DatePicker的使用 android 开发 时间选择器TimePicker的使用

0 人点赞