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

示意图:

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

layout布局文件:xml




    
        

        
        

        
        
        
        
        
        
    


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

    }
}

你可能感兴趣的:(Android,相关,android,gitee)