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

示意图:

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();

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android时间联动是指在Android应用中,不同的时间组件可以相互关联,通过互相监听和响应,实现统一的时间变化效果。 Android中常见的时间组件包括DatePicker日期选择器)、TimePicker时间选择器)和CalendarView日期日历视图)等。这些组件可以根据应用需求分别单独使用,也可以进行联动使用。 比如,当用户在DatePicker选择日期时,可以通过监听DatePicker的OnDateChangedListener回调方法,获取用户选择的年、月、日信息。然后可以将这些信息传递给后续的操作,比如更新显示的时间、保存选中的日期等。 类似地,当用户在TimePicker选择时间时,可以通过监听TimePicker的OnTimeChangedListener回调方法,获取用户选择的小时、分钟信息。再次,可以根据这些信息进行后续的操作,比如更新显示的时间、保存选中的时间等。 除了单独使用之外,这些时间组件还可以进行联动使用。例如,当用户在DatePicker选择日期时,可以通过设置TimePicker的默认时间为当前日期,将用户选择日期和现在的时间进行关联。这样用户就可以在选择日期的同时,也能够看到对应的时间。 总之,Android时间联动可以通过监听不同的时间组件的回调方法,根据用户在其中做出的不同选择,实现对其他时间组件的联动操作和显示更新。这样可以提升用户体验,方便用户在应用中准确地选择和展示时间信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaGHui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值