技海泛舟(个人技术研究)

  • 首页
  • 日积月累
  • 学习计划
  • 随想
  • project
  • 关于
技海泛舟
一个技术宅的博客
  1. 首页
  2. 学习计划
  3. 正文

Android日期时间对话框

2022年11月6日 1410点热度
  • 日期选择器DatePicker,可以让用户选择具体的年月日

  • 日期选择器DatePicker并非弹框模式,而是在当前页面占据一块区域,并且不会自动关闭

  • 日期对话框DatePickerDialog相当于在AlertDialog上装载了DatePicker,日期选择事件则由监听器onDateSetListener负责响应,在该监听器的onDateSet方法中,获取用户选择的具体日期,再做后续处理。

  • 当 android:datePickerMode 属性设置为 spinner 时可以选择 年月日

  • 当 android:datePickerMode 属性设置为 calendar 会显示一个日历选择器
    android:calendarTextColor 日历列表的文本的颜色
    android:calendarViewShown 是否显示日历视图
    android:datePickerMode 组件外观,可选值:
    spinner,
    calendar 默认
    android:dayOfWeekBackground 顶部星期几的背景颜色
    android:dayOfWeekTextAppearance 顶部星期几的文字颜色
    android:endYear 结束年份,比如 2017
    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 设置第一年(内容),比如 1940 年
    android:yearListItemTextAppearance 列表的文本出现在列表中
    android:yearListSelectorColor 年列表选择的颜色

  • DatePicker 没有提供专门的方法用于获得它的值,如果要获得值,只能通过 Calendar.getInstance().get() 方法分别获取年月日的值,然后在组装

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <DatePicker

            android:id="@+id/datePicker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:calendarViewShown="false"
            android:datePickerMode="spinner"></DatePicker>

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="时间选择确定"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/datePicker"></Button>
 <Button
            android:id="@+id/btn_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="弹出选择框——时间选择"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn_ok"></Button>

        <TextView
            android:id="@+id/tv_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="选择的时间为:"
            app:layout_constraintTop_toBottomOf="@+id/btn_date">

        </TextView>

        <TextView
            android:id="@+id/tv_date2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="onDateChange监听选择时间为:"
            app:layout_constraintTop_toBottomOf="@+id/tv_date"></TextView>
    </androidx.constraintlayout.widget.ConstraintLayout>
        // 给时间选择按钮注册监听事件
        findViewById(R.id.btn_ok).setOnClickListener(this::onClickTimeFn);
        // 获取时间选择器
        dp_datePicker = findViewById(R.id.datePicker);
        tv_date = findViewById(R.id.tv_date);
        tv_date2 = findViewById(R.id.tv_date2);
//        dp_datePicker.setOnDateChangedListener(this::dateFn);

        findViewById(R.id.btn_date).setOnClickListener(this::onClickTimeFn);

    }

    private void onDateSetFn(DatePicker datePicker, int year, int month, int dayOfMonth) {
//        int currentYear = dp_datePicker.getYear();
//        int month = dp_datePicker.getMonth() + 1;
//        int day = dp_datePicker.getDayOfMonth();
//        tv_date2.setText(String.format("setOnDateChangedListener监听的日期是%d年%d月%日", currentYear, month, day));
        String desc = String.format("您选择的日期是%d年%d月%d日", year, month+1, dayOfMonth);
        tv_date.setText(desc);
    }

    private void onClickTimeFn(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                String desc = String.format("您选择的日期是%d年%d月%d日", dp_datePicker.getYear(), dp_datePicker.getMonth() + 1, dp_datePicker.getDayOfMonth());
                tv_date.setText(desc);
                break;
            case R.id.btn_date:
                //  获取日历实例,包含当前的年月日
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog = new DatePickerDialog((Context) this, (DatePickerDialog.OnDateSetListener) this::onDateSetFn, year, month, dayOfMonth);
                // 显示日期对话框
                dialog.show();

                break;
        }
    }
  • 时间选择器TimePicker 可以让用户选择具体的小时和分钟
  • 时间选择框TimePickerDialog的用法类似。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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=".TimePickerActivity">
    
    <TextView
        android:id="@+id/tv_time"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="您选择的时间是:"
        tools:ignore="MissingConstraints"></TextView>
    
    <TimePicker
        android:id="@+id/tp_time"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_time"
        tools:ignore="MissingConstraints"></TimePicker>
    <TimePicker
        android:id="@+id/tp_time2"
    
      android:timePickerMode="spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tp_time"
        tools:ignore="MissingConstraints"></TimePicker>
    
    <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    
        android:text="选择后确定"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tp_time2"
        tools:ignore="MissingConstraints"></Button>
    
    <Button
        android:id="@+id/btn_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    
        android:text="时间对话框"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_ok"
        app:layout_constraintTop_toBottomOf="@+id/tp_time2"
        tools:ignore="MissingConstraints"></Button>
    </androidx.constraintlayout.widget.ConstraintLayout>
    private TextView tv_time;
    private TimePicker timepicker2;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);

        TimePicker timepicker = (TimePicker) findViewById(R.id.tp_time);
        timepicker2 = (TimePicker) findViewById(R.id.tp_time2);

//        通过 setIs24HourView(true) 方法改成 24 小时制
        timepicker.setIs24HourView(true);
        timepicker2.setIs24HourView(true);
        tv_time = findViewById(R.id.tv_time);

        findViewById(R.id.btn_ok).setOnClickListener(this::customClickFn);
        findViewById(R.id.btn_dialog).setOnClickListener(this::customClickFn);
    }

    private void customClickFn(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                String desc = String.format("您选择是时间是%d时%d分",timepicker2.getHour(),timepicker2.getMinute());
                tv_time.setText(desc);
                break;
            case R.id.btn_dialog:
                // 获取日历的一个实例,获取当前的时分秒(采用单例模式)
                Calendar calendar =  Calendar.getInstance();
                // 构建时间对话框,
                TimePickerDialog dialog = new TimePickerDialog((Context) this,android.R.style.Theme_Holo_Light_Dialog, (TimePickerDialog.OnTimeSetListener) this::customTimeSetFn,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true);
                dialog.setTitle("pick");
                      dialog.show();
                break;
        }
    }

    private void customTimeSetFn(TimePicker timePicker, int hourOfDay, int minute) {
        String desc = String.format("您选择是时间是%d时%d分",hourOfDay,minute);
        tv_time.setText(desc);
    }

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2022年11月8日
< 上一篇
下一篇 >
归档
  • 2024 年 11 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2024 年 2 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 9 月
  • 2023 年 6 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 8 月
  • 2022 年 7 月
  • 2022 年 6 月
  • 2022 年 5 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 5 月
分类
  • Android
  • Arduino
  • cordova
  • css
  • go
  • html5
  • JavaScript
  • nodejs
  • oracle
  • project
  • system
  • uni-app
  • vscode
  • vue
  • 学习计划
  • 摘抄
  • 随想
最新 热点 随机
最新 热点 随机
windows安装mysql ,VSCODE连接MySQL数据库 创建api的逻辑 观看七战奥运会 德国波尔告别 go utils工具 Go语言中的sort包帮我们实现了对任一类型的数组进行排序。 vue + go安装
读阮一峰老师-穷忙的人生 将Node.js应用程序编译成一个.exe文件 第五章 css 定位布局 GO fundamentals of programming 01 canvascanvas 科技动画背景 、图板填鸭及切换画笔、擦除、清除、保存 http协议

COPYRIGHT © 技海泛舟(个人技术研究). 2021-2023. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

黑ICP备19002110号-1

黑公网安备 23060202000432号