android13打基础:控件datepicker

datepicker测试activity

java 复制代码
// todo: 高级控件日期选择datepicker
public class Ch4_DatePickerActivity extends AppCompatActivity
        implements View
        .OnClickListener, DatePickerDialog.OnDateSetListener {

    private TextView tv_date; // 声明一个文本视图对象
    private DatePicker dp_date; // 声明一个日期选择器对象

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


        tv_date = findViewById(R.id.ch4_tv_date);
        dp_date = findViewById(R.id.ch4_dp_date);
        // 按钮点击事件
       findViewById(R.id.ch4_btn_date).setOnClickListener(this);
       findViewById(R.id.ch4_btn_ok).setOnClickListener(this);
    }

    @Override // 一旦点击日期对话框上的确定按钮,就会触发监听器的onDateSet方法
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        // 获取日期对话框设定的年月份
        String desc = String.format("您选择的日期是%d年%d月%d日",
                year, month + 1, dayOfMonth);
        tv_date.setText(desc);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ch4_btn_date) {
            // 获取日历的一个实例,里面包含了当前的年月日
            Calendar calendar = Calendar.getInstance();
            // 构建一个日期对话框,该对话框已经集成了日期选择器。
            // DatePickerDialog的第二个构造参数指定了日期监听器
            DatePickerDialog dialog = new DatePickerDialog(this, this,
                    calendar.get(Calendar.YEAR), // 年份
                    calendar.get(Calendar.MONTH), // 月份
                    calendar.get(Calendar.DAY_OF_MONTH)); // 日子
            dialog.show(); // 显示日期对话框
        } else if (v.getId() == R.id.ch4_btn_ok) {
            // 获取日期选择器dp_date设定的年月份
            String desc = String.format("您选择的日期是%d年%d月%d日",
                    dp_date.getYear(), dp_date.getMonth() + 1, dp_date.getDayOfMonth());
            tv_date.setText(desc);
        }
    }
}

布局文件

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/ch4_btn_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择日期"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <!-- datePickerMode取值spinner表示下拉框风格,取值calendar表示日历风格 -->
    <DatePicker
        android:id="@+id/ch4_dp_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        android:gravity="center"
        android:spinnersShown="true" />

    <Button
        android:id="@+id/ch4_btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="确  定"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/ch4_tv_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="@color/black"
        android:textSize="17sp" />


</LinearLayout>

点击日期选择器

相关推荐
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白2 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快4 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo19 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks19 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui1 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android2 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m2 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui2 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android