Android DatePicker(日期选择器)、TimePicker(时间选择器)、CalendarView(日历视图)- 简单应用

news/2024/5/17 20:09:01 标签: android, gitee

示意图:

layout布局文件:xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="DatePicker(日期选择器):"
            android:textSize="24sp"
            />

        <DatePicker
            android:id="@+id/btnDp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        <DatePicker
            android:calendarTextColor="#ff00ff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:datePickerMode="spinner"
            android:headerBackground="#ff00ff00"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TimePicker(时间选择器)"
            android:textSize="24sp"
            android:gravity="center"
            />
        <TimePicker
            android:id="@+id/btnTp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CalendarView(日历视图)"
            android:textSize="24sp"
            android:gravity="center"
            />
        <CalendarView
            android:id="@+id/btnCv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        
    </LinearLayout>


</ScrollView>

MainActivity:

package com.example.mydate;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements  DatePicker.OnDateChangedListener,
         CalendarView.OnDateChangeListener,TimePicker.OnTimeChangedListener{
    //定义组件 都继承 FrameLayout 帧布局
    private DatePicker datePicker;

    private TimePicker timePicker;
    
    private CalendarView calendarView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datePicker = findViewById(R.id.btnDp);
        timePicker = findViewById(R.id.btnTp);
        calendarView = findViewById(R.id.btnCv);
        //绑定事件 日期
        datePicker.setOnDateChangedListener(this);

        //时间
        timePicker.setOnTimeChangedListener(this);
        
        //日期
        calendarView.setOnDateChangeListener(this);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Toast.makeText(this,year+"-年-"+(monthOfYear+1)+"-月-"+dayOfMonth+"-日",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(this,hourOfDay+"-小时"+minute+"-分钟-",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
        Toast.makeText(this,"您选择了"+year+"-年-"+(month+1)+"-月-"+dayOfMonth+"-日",Toast.LENGTH_SHORT).show();

    }
}


http://www.niftyadmin.cn/n/5202052.html

相关文章

基于Android个人理财家庭收支系统uniAPP+vue -hbuiderx-微信小程序vj9n8

摘 要 个人理财APP设计的目的是为用户提供对活动信息和提醒信息管理等方面的平台。 与PC端应用程序相比&#xff0c;个人理财的设计旨在为用户提供一个个人理财信息的管理。用户可以通过APP及时收入信息和支出信息&#xff0c;并对提醒消息进行查看等。 个人理财APP是在Androi…

php一句话木马免杀

php一句话木马免杀 针对于php一句话木马做免杀&#xff1a; 利用php动态函数的特性&#xff0c;将危险函数拆分成字符&#xff0c;最终使用字符串拼接的方式&#xff0c;然后重新拼接&#xff0c;后加括号执行代码&#xff0c;并且可以使用花指令进行包装&#xff0c;如无限i…

前端为什么不能直接连数据库

其实也不是不可以&#xff0c;只是这样做有很多不好的地方&#xff0c;但是如果是一个只有几个人用的内网小系统&#xff0c;是没有问题的。主要基于以下原因考虑 安全性问题&#xff1a;前端信息都是公开的&#xff0c;从前端访问数据库&#xff0c;就需要将数据库的地址&…

【Windows 常用工具系列 11 -- win11怎么设置不睡眠熄屏 |win11设置永不睡眠的方法】

文章目录 win11 怎么设置不睡眠熄屏 使用笔记本电脑的时候&#xff0c;如果离开电脑时间稍微长一点就会发现息屏了&#xff0c;下面介绍 设置 Win11 永不睡眠息屏的方法&#xff0c;有需要的朋友们快来看看以下详细的教程。 win11 怎么设置不睡眠熄屏 在电脑桌面上&#xff0c…

git -1

1.创建第一个仓库并配置local用户信息 git config git config --global 对当前用户所有仓库有效 git config --system 对系统所有登录的用户有效 git config --local 只对某个仓库有效 git config --list 显示配置 git config --list --global 所有仓库 git config --list…

django+drf+vue 简单系统搭建 (4) 用户权限

权限控制是web中的重要组成部分。与以往的博客系统不同&#xff0c;本次工具页面仅支持注册用户。 每个注册用户都能访问到工具页面&#xff0c;并且提交自己的task来选择具体的工具来处理自己提交的文件。每个注册用户都只能访问到自己提交的task&#xff0c;而管理员则可以查…

【密文特征分析】加密类型判断

1、代码 import re import sysimport requestsdef post_url(data):"""其实就是把「https://www.dcode.fr/cipher-identifier」网站的功能&#xff0c;改成接口形式之所以改成接口&#xff0c;是因为网站加载有些慢&#xff0c;很多不必要加载很是浪费时间:para…

机器视觉系统选型-环形光源分类及应用场景

环形光源主要分为&#xff1f; 1.环形光源(高角度) 照射光线与水平方向成高角度夹角 外观缺陷检测字符识别PCB基板检测二维码读取- 2.环形光源(低角度) 照射光线与水平方向成低角度夹角各种边缘提取字符识别玻璃断面的损伤检测金属表面刻印、损伤 3.环形光源(高亮) 高亮度远…